In a previous blog, we provided an overview of basic data structures in R. In this follow up piece, we will provide a snapshot of basic syntax in R for programmers who want to get up to speed in this increasingly important programming language.
Like other programming languages, R too has provision for assignment of values to variables, conditional statements and loops.
In R, "<-" and "=" are both used for assignment of values to variables. The difference between “=” and “<-“ is mainly related to the scope of the variable.
When we use the “=” operator, the variable x is only defined within the function, i.e., its scope is limited to the function definition. On the other hand, when we use the “<-“ operator the variable is also available outside the function, i.e., x exists in the memory even after the end of the function call.
In R, Conditional Statements consist of Comparison operators ==, !=, >, <, >=, <= Logical operators | (OR), & (AND), and if-else conditions. The example below demonstrates the use of an If- else condition.
When starting out in R, it is useful to have a basic understanding of loops and how to write them. Below we provide an example of a 'for' loop and a 'while' loop. However, in R, a variety of apply functions, to be covered in future blogs, tend to work more efficiently than the ‘for’ and ‘while’ loops in most situations.
In R, user-deﬁned functions may be used for repetitive executions in a similar way to macros in SAS.The return value could be of any type - a single value, vector, data frame or a list.
Formulae are an efficient way of specifying a model or inputs for many functions in R. In the example below, a formula is used to specify a linear model.
In a future blog we'll go on to look at missing values and debugging in R.
Want to learn more? Further related reading is below:
With many thanks to Cytel's Namrata Deshpande and Imran Hossein.
Liked this? Join our community of biopharma innovators and sign up for Cytel blog updates direct to your inbox.