Syntax and Variables in R: A Primer

Posted by Cytel

Feb 21, 2017 9:39:07 AM

 2computers.jpg

 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. 

Assignment operator

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.

 

 Assignmentoperator1.png

 

 
assignmentoperations2.png

 

 Conditional Statements

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.

conditionalstatements1.png

Loops

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.

loops.png

 Functions
In R, user-defined 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. 

functions.png

 

Formulae

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. 

formulae.png

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:

 Data Structures in R: A Primer

The Rise of R- is it time for SAS programmers to get up to speed?

Harnessing the power of R API

The evolving role of the modern statistical programmer

 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.

Subscribe

 

 

Topics: Statistical Programming, biostatistics, SAS, R

The Cytel blog keeps you up to speed with the latest developments in biostatistics and clinical biometrics.  Sign up for updates direct to your inbox. You can unsubscribe at any time.

 

Posts by Topic

see all

Recent Posts