{"id":1800,"date":"2020-12-05T17:41:30","date_gmt":"2020-12-05T17:41:30","guid":{"rendered":"https:\/\/ded9.com\/?p=1800"},"modified":"2025-12-13T11:54:30","modified_gmt":"2025-12-13T11:54:30","slug":"basic-programming-syntax-training-r","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/basic-programming-syntax-training-r\/","title":{"rendered":"Basic Programming Syntax Training in R: Learn the Fundamentals"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">For ease of use,\u00a0learn R programming by writing &#8220;<a href=\"https:\/\/ded9.com\/learn-hello-world-in-java-in-very-simple-language\/\">Hello World<\/a>!&#8221; Let&#8217;s start. Depending on your needs, you can write your program, execute it in R, or use an R script for this purpose.<\/span><\/p>\n<h2>Basic Programming Syntax Training R<\/h2>\n<h3><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258577 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/learn-r.webp\" alt=\"Basic Programming Syntax Training R\" width=\"1440\" height=\"1080\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/learn-r.webp 1440w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/learn-r-300x225.webp 300w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/learn-r-1024x768.webp 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/learn-r-768x576.webp 768w\" sizes=\"(max-width: 1440px) 100vw, 1440px\" \/><\/h3>\n<p>Syntax training in R: Let&#8217;s look at both.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">$ R<\/pre>\n<\/div>\n<p>This will launch R interpreter, and you will get to the prompt&gt; where you can type your program as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">&gt; myString &lt;- \u201cHello, World!\u201d\r\n\r\n&gt; print (myString)\r\n\r\n[1] \u201cHello, World!\u201d<\/pre>\n<\/div>\n<p>Here, the first expression defines a <a href=\"https:\/\/en.wikipedia.org\/wiki\/String_(computer_science)\" target=\"_blank\" rel=\"noopener\">String<\/a> variable and assigns the phrase &#8220;Hello World&#8221; to it. Then, the print() statement is used to print the value stored in the myString variable.<\/p>\n<h2><span style=\"font-size: 18pt;\">R Script File<\/span><\/h2>\n<p>Typically, you will write your programs in script files and then use the R interpreter, which is invoked by Rscript, to run those scripts in the Command prompt. So, let&#8217;s proceed as follows: Start by writing the following code in the text File that tests. R calls:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># My first program in R Programming\r\n\r\nmyString &lt;- \u201cHello, World!\u201d\r\n\r\nprint (myString)<\/pre>\n<\/div>\n<p>Save the above code to a File named test.. Run it on the Linux Command line as shown below. Even if you use Windows or other systems, the syntax you use is the same.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">$ Rscript test.R<\/pre>\n<\/div>\n<p>When we run the above program, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[1] \"Hello, World!\"<\/pre>\n<\/div>\n<h3><span style=\"font-size: 14pt;\">Comments<\/span><\/h3>\n<p>Comments are the same as the help text in your R program while it is running; the program interpreter ignores these comments. Single line comment like below: Using # at the beginning of the phrase; It is written:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># My first program in R Programming<\/pre>\n<\/div>\n<p>R programming does not support multi-line comments, but you can use a trick that looks something like this:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">if (FALSE) {<\/pre>\n<\/div>\n<p dir=\"ltr\">&#8220;This is a demo for multi-line comments, and it should be put inside either a<\/p>\n<p dir=\"ltr\">single OR double quote.&#8221;<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">}\r\n\r\nmyString &lt;- \u201cHello, World!\u201d\r\n\r\nprint (myString)\r\n\r\n[1] \u201cHello, World!\u201d<\/pre>\n<\/div>\n<p>Although the R program interpreter executes the above comments, they do not interfere with your actual program. You should put these types of remarks either in one quote or in two quotes.<\/p>\n<h2><strong>R &#8211; Basic Syntax<\/strong><\/h2>\n<p>To begin learning R programming, we&#8217;ll start with a classic <strong>&#8220;Hello, World!&#8221;<\/strong>\u00a0example. You can write and execute R code in two ways:<\/p>\n<ol start=\"1\">\n<li><strong>Directly in the R interactive prompt<\/strong><\/li>\n<li><strong>Using an R script File<\/strong><\/li>\n<\/ol>\n<h3><strong>1. R Command Prompt<\/strong><\/h3>\n<p>After setting up the R environment, launch the\u00a0<strong>R interpreter<\/strong> by entering:<\/p>\n<p>You&#8217;ll see the <code>&gt;<\/code> prompt, where you can type and execute R code:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">&gt; myString &lt;- \"Hello, World!\" &gt; print(myString) [1] \"Hello, World!\"<\/pre>\n<\/div>\n<\/div>\n<ul>\n<li><code>myString<\/code> It&#8217;s a variable storing the text <code>\"Hello, World!\"<\/code>.<\/li>\n<li><code>print()<\/code> Displays the value of the variable.<\/li>\n<\/ul>\n<h3><strong>2. R Script File<\/strong><\/h3>\n<p>For more extensive programs, save your code in a script File (e.g., <code>test.R<\/code>) and run it using\u00a0<strong>Rscript<\/strong>:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># My first R program myString &lt;- \"Hello, World!\" print(myString)<\/pre>\n<\/div>\n<\/div>\n<h4><strong>Running the Script<\/strong><\/h4>\n<p>At the terminal (Linux\/macOS\/Windows), execute:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">$ Rscript test.R<\/pre>\n<\/div>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[1] \"Hello, World!\"<\/pre>\n<\/div>\n<\/div>\n<hr \/>\n<h3><strong>3. Comments in R<\/strong><\/h3>\n<p>Comments help document code and are ignored during execution.<\/p>\n<h4><strong>Single-Line Comments<\/strong><\/h4>\n<p>Use\u00a0<code>#<\/code>:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># This is a comment<\/pre>\n<\/div>\n<\/div>\n<h4><strong>Multi-Line Comments (Workaround)<\/strong><\/h4>\n<p>Since R doesn&#8217;t support multi-line comments directly, you can use:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">if(FALSE) { \"This is a pseudo multi-line comment. It\u2019s enclosed in quotes but never executed.\" }<\/pre>\n<\/div>\n<\/div>\n<p><strong>Note:<\/strong>\u00a0The\u00a0<code>if(FALSE)<\/code> The block prevents execution, effectively acting like a comment.<\/p>\n<p>This guide covers the basics of R syntax, execution methods, and commenting. Please let me know if you&#8217;d like any modifications.<\/p>\n<h2><strong>Functions in R<\/strong><\/h2>\n<h3><strong>Introduction to Functions<\/strong><\/h3>\n<p>In R, functions are fundamental objects that (typically) take arguments and return values. These values can be any R object, including other functions. Let&#8217;s explore how functions work using examples.<\/p>\n<h4><strong>Basic Function Example:\u00a0<code>sqrt()<\/code><\/strong><\/h4>\n<p>The\u00a0<code>sqrt()<\/code> Function calculates the square root of its input:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">sqrt(2) # [1] 1.414214<\/pre>\n<\/div>\n<\/div>\n<h4><strong>Vectorized Operations<\/strong><\/h4>\n<p>R functions often work on entire vectors:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">sqrt(1:10) # Applies sqrt to each element from 1 to 10 # [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 # [9] 3.000000 3.162278<\/pre>\n<\/div>\n<\/div>\n<h4><strong>Error Handling<\/strong><\/h4>\n<p>Functions produce errors or warnings for invalid inputs:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">sqrt(\"hello world\") # Error: non-numeric argument sqrt(-1) # Warning: NaN produced (no real square root) # [1] NaN<\/pre>\n<\/div>\n<\/div>\n<h4><strong>Function Arguments<\/strong><\/h4>\n<p>Many functions accept multiple arguments, some of which are optional.<\/p>\n<p><strong>Example:\u00a0<code>round()<\/code><\/strong><\/p>\n<p>The\u00a0<code>round()<\/code> Function formats numbers to the specified decimal places:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">round(pi) # Default (0 decimal places) # [1] 3 round(pi, 2) # Round to 2 decimal places (positional argument) # [1] 3.14 round(pi, digits = 2) # Same as above (named argument) # [1] 3.14<\/pre>\n<\/div>\n<\/div>\n<p><strong>Key Points About Arguments:<\/strong><\/p>\n<ol start=\"1\">\n<li><strong>Positional Passing<\/strong>: Arguments can be given in order (<code>round(pi, 2)<\/code>).<\/li>\n<li><strong>Named Passing<\/strong>: Explicitly naming arguments (<code>digits = 2<\/code>) improves readability.<\/li>\n<li><strong>Default Values<\/strong>: If omitted,\u00a0<code>digits<\/code>\u00a0defaults to\u00a0<code>0<\/code>.<\/li>\n<\/ol>\n<h2><strong>Exploring Function Arguments<\/strong><\/h2>\n<p>To quickly check a function&#8217;s arguments, use\u00a0<code>args()<\/code>:<\/p>\n<div class=\"md-code-block md-code-block-light\">\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">args(round) # function (x, digits = 0) # NULL<\/pre>\n<\/div>\n<\/div>\n<p>This shows that\u00a0<code>round()<\/code>\u00a0requires\u00a0<code>x<\/code>\u00a0(the number) and optionally accepts\u00a0<code>digits<\/code>.<\/p>\n<h3><strong>Other Help Tools<\/strong><\/h3>\n<ul>\n<li><code>?round<\/code>\u00a0or\u00a0<code>help(round)<\/code>Detailed documentation.<\/li>\n<li><code>??round<\/code>Searches broader help topics.<\/li>\n<\/ul>\n<h2><strong>Summary<\/strong><\/h2>\n<ul>\n<li>Functions process inputs and return outputs.<\/li>\n<li>Many functions are\u00a0<strong>vectorized, meaning they <\/strong>work on entire vectors.<\/li>\n<li>Arguments can be passed\u00a0<strong>positionally<\/strong>\u00a0or\u00a0<strong>by name<\/strong>.<\/li>\n<li>Use\u00a0<code>args()<\/code>,\u00a0<code>?<\/code>, or\u00a0<code>help()<\/code>\u00a0to explore functions.<\/li>\n<\/ul>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is R programming mainly used for?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>R is primarily used for data analysis, statistics, and data visualization.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What topics are covered in basic R syntax training?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It includes variables, data types, operators, conditional statements, loops, and functions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Is R a good language for beginners?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, especially for those interested in data analysis and statistical programming.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>For ease of use,\u00a0learn R programming by writing &#8220;Hello World!&#8221; Let&#8217;s start. Depending on your needs, you can write your program, execute it in R, or use an R script for this purpose. Basic Programming Syntax Training R Syntax training in R: Let&#8217;s look at both. $ R This will launch R interpreter, and you [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":258580,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[174],"tags":[3324],"class_list":["post-1800","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-r-studio","tag-r"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/1800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=1800"}],"version-history":[{"count":5,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/1800\/revisions"}],"predecessor-version":[{"id":266101,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/1800\/revisions\/266101"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/258580"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}