Raymond Raymond

R Programming Basics

event 2020-09-23 visibility 183 comment 0 insights toc
more_vert
insights Stats

This article provides a basic introduction about programming with R incl. atomic vector, variable, operations, branching, loops and functions. 

infoAll examples can run RStudio or R Tools for Visual Studio on Windows.  About these two IDEs, refer to R Introduction.

Hello, World!

We always start with a 'Hello World!' example when learning a new programming language.

The following is the content of an example R script file named R01.HelloWorld.R.

# Set working directory
setwd("F:/Projects/RTutorialSamples")

# This is a quick demo of R
print ('Hello, World!')

myVar <- "Hello, World!"

print(myVar)

Output:

> # Set working directory
> setwd("F:/Projects/RTutorialSamples")
> 
> # This is a quick demo of R
> print ('Hello, World!')
[1] "Hello, World!"
> 
> myVar <- "Hello, World!"
> 
> print(myVar)
[1] "Hello, World!"
> 

Functions

There are two functions used in the above script, print and setwd.

Function setwd sets the current working directory to the path provided. Once the directory is setup, working space related files like .RData or .Rhistory can be saved in this directory. Relative paths will be resolved using working directory path. 

Function print simply prints out the input string or string variable to the output stream/console.

Variable

As other programming languages, you can define variables in R. The above script defines a variable named myVar and assigns a string literal 'Hello, World!" to it. 

Comments

To add comments into your R script, add character '#' to the comment line as the above script example shows. 

Run R scripts using command line

R scripts can be invoked using Rscript.exe executable:

Rscript.exe R01.HelloWorld.R

Atomic vector

R is created for statistical computing and graphics thus there is a core difference compared with other programming languages about vectors. A vector is a sequence of data elements of the same basic type. Members in a vector are called components.

In R, the following atomic vectors are available:

  • Logical
  • Numeric
  • Integer
  • Complex
  • Character
  • Raw

All other objects in R are created with these basic vectors. 

Examples

The following script R02.AtomicVectors.R shows some examples of vectors in R:

# Example R02 Basic Vectors

# logical
v <- TRUE
print(v)
print(class(v))

# numeric 
v <- 99.99
print(v)
print(class(v))

# integer
v <- 100L
print(v)
print(class(v))

# complex

v <- 10 -3i
print(v)
print(class(v))

# character

v <- 'i am a character'
print(v)
print(class(v))

Variables

As mentioned in the 'Hello World!' example, variables are used to store values. Variable value can be access through the identifier (variable name). Variable naming in R needs to following some standards:

  • Consists of letters, numbers, . , or _ characters
  • Starts with a letter or the dot not followed by a number

To assign values to variables, the following operators can be used:

  • <-
  • ->
  • <<-

To list all the variables in the current environment:

ls()
ls(all.names = TRUE)

To find variables with specific patterns:

ls(pattern="pat")
To delete variables, use rm function:
rm
rm(list=ls())

Examples

The following code script R03.Variables.R shows some examples of using variables:

atomicV<-"Jan"
print(v)
print(class(v))
print(length(v))

c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") -> v
print(v)
print(class(v))
print(length(v))

v2 <- 2
.myv <- TRUE

ls()

ls(all.names = TRUE)

ls(pattern = "v")

ls(pattern = "v", all.names = TRUE)

ls(pattern = "^v", all.names = TRUE)

ls(pattern = "v$", all.names = TRUE)

rm(v)

rm(list=ls())
Sample output
> atomicV<-"Jan"
> print(v)
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> print(class(v))
[1] "character"
> print(length(v))
[1] 12
> 
> c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") -> v
> print(v)
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> print(class(v))
[1] "character"
> print(length(v))
[1] 12
> 
> v2 <- 2
> .myv <- TRUE
> 
> ls()
 [1] "atomicV"          "cdt"              "colors"           "data"            
 [5] "floor"            "GCtorture"        "myVar"            "output.forest"   
 [9] "output.tree"      "result"           "result1"          "result2"         
[13] "sales"            "sales.timeseries" "sales1"           "sales2"          
[17] "sales3"           "sf"               "v"                "v2"              
[21] "X"               
> 
> ls(all.names = TRUE)
 [1] ".myv"             ".Random.seed"     "atomicV"          "cdt"             
 [5] "colors"           "data"             "floor"            "GCtorture"       
 [9] "myVar"            "output.forest"    "output.tree"      "result"          
[13] "result1"          "result2"          "sales"            "sales.timeseries"
[17] "sales1"           "sales2"           "sales3"           "sf"              
[21] "v"                "v2"               "X"               
> 
> ls(pattern = "v")
[1] "v"  "v2"
> 
> ls(pattern = "v", all.names = TRUE)
[1] ".myv" "v"    "v2"  
> 
> ls(pattern = "^v", all.names = TRUE)
[1] "v"  "v2"
> 
> ls(pattern = "v$", all.names = TRUE)
[1] ".myv" "v"   
> 
> rm(v)
> 
> rm(list=ls())

Operator 

A R operator is a symbol that instructs the compiler to perform specific mathematical or logical manipulations. The following types of operators are available in R:

  • Arithmetic
  • Relational
  • Logical
  • Assignment
  • Others

The following are some typical operators for each category. Refer to R official manual for the meaning of each of them. 

Operator categoryOperators
Arithmetic+, _, *,/, %%, %/%, ^
Relational >, <, ==, <=, >=, !=
Logical& (element wise), | (element wise), !, &&, ||
Assignment<-, ->, =, <<- (global variable), ->>
Others:, %in%, %*% (transpose)

Examples

The following code script R04.Operators.R provides some examples of using these operators:

### Arithmetic operators
a <- 15
b <- 2
a+b
a-b
a*b
a/b
#Mod
a%%b
#Divide (quotient)
a%/%b
a^b

# vector with more than one element
x<- c(1,2,3)
y<- c(4,5,6)
x+y

### Relational operators
x>=y
x>y
x<y
x!=y
x==y

### Logical operators
x&y
x|y
!x
!y
# Single output
x&&y
x||y

### Assignment operators
c('a',1,TRUE,5+5i) ->> v2

### Other operators
v <- 1:100
print(v)

1 %in% v
101 %in% v

# multiply a matrix with its transpose
mx = matrix(1:10,nrow=2, ncol=5)
View(mx)
tmx = t(mx)
View(tmx)

mx %*% tmx

Sample output
> ### Arithmetic operators
> a <- 15
> b <- 2
> a+b
[1] 17
> a-b
[1] 13
> a*b
[1] 30
> a/b
[1] 7.5
> #Mod
> a%%b
[1] 1
> #Divide (quotient)
> a%/%b
[1] 7
> a^b
[1] 225
> 
> # vector with more than one element
> x<- c(1,2,3)
> y<- c(4,5,6)
> x+y
[1] 5 7 9
> 
> ### Relational operators
> x>=y
[1] FALSE FALSE FALSE
> x>y
[1] FALSE FALSE FALSE
> x<y
[1] TRUE TRUE TRUE
> x!=y
[1] TRUE TRUE TRUE
> x==y
[1] FALSE FALSE FALSE
> 
> ### Logical operators
> x&y
[1] TRUE TRUE TRUE
> x|y
[1] TRUE TRUE TRUE
> !x
[1] FALSE FALSE FALSE
> !y
[1] FALSE FALSE FALSE
> # Single output
> x&&y
[1] TRUE
> x||y
[1] TRUE
> 
> ### Assignment operators
> c('a',1,TRUE,5+5i) ->> v2
> 
> ### Other operators
> v <- 1:100
> print(v)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
 [21]  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40
 [41]  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60
 [61]  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80
 [81]  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100
> 
> 1 %in% v
[1] TRUE
> 101 %in% v
[1] FALSE
> 
> # multiply a matrix with its transpose
> mx = matrix(1:10,nrow=2, ncol=5)
> View(mx)
> tmx = t(mx)
> View(tmx)
> 
> mx %*% tmx
     [,1] [,2]
[1,]  165  190
[2,]  190  220

For the matrix and transposed matrix, the output looks like the following:

mx:

2020092313713-image.png

tmx:

2020092313732-image.png

Branching

Branching are commonly used in programming and in R the following are the commonly used ones:

  • if
  • if .. else
  • switch

The following flow chart shows in high-level how it works:

2020092314127-image.png

Examples

The following code script R05.Branching.R provides some examples of using these branching features:

rm(list=ls())

# Branching if else switch

a <- 2
b <- 3

if(a>b)
{
  print("a is greater than b")
}else
{
  print("a is not greater than b")
}


# switch


switch(
  5,
  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
)
Sample output
[1] "a is not greater than b"
> 
> 
> # switch
> 
> 
> switch(
+   5,
+   "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
+ )
[1] "May"

Loops and loop control

R supported loops include: repeat, while, for.  The following flow chart shows how that works in high-level:

2020092314637-image.png

Loop control language features include break and next. Break will break the current entire loop while next will exit and go to next iteration only. 

Examples

The following code script R06.Loops.R provides some examples of using loops and breaks:

# Loops
i <- 1
repeat
{
  print('I am repeating')
  i = i+1
  if(i>10)
    break
}


j<-1
while(j<=10)
{
  print('I am repeating')
  j = j+1
}

v<-1:10
for(i in v)
{
  print('I am repeating')
}

# only print every two loops

v<-1:10
for(i in v)
{
  print(i)
  if(i%%2 != 0)
    next
  
  print('I am repeating')
}
Sample output
> # Loops
> i <- 1
> repeat
+ {
+   print('I am repeating')
+   i = i+1
+   if(i>10)
+     break
+ }
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
> 
> 
> j<-1
> while(j<=10)
+ {
+   print('I am repeating')
+   j = j+1
+ }
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
> 
> v<-1:10
> for(i in v)
+ {
+   print('I am repeating')
+ }
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
[1] "I am repeating"
> 
> # only print every two loops
> 
> v<-1:10
> for(i in v)
+ {
+   print(i)
+   if(i%%2 != 0)
+     next
+   
+   print('I am repeating')
+ }
[1] 1
[1] 2
[1] "I am repeating"
[1] 3
[1] 4
[1] "I am repeating"
[1] 5
[1] 6
[1] "I am repeating"
[1] 7
[1] 8
[1] "I am repeating"
[1] 9
[1] 10
[1] "I am repeating"

Functions

In the above examples, we used functions several times. A function in R is a set of statements to perform certain task. A function is also one object in R. 

Function are created using these components:

  • Name
  • Arguments
  • Body
  • Return Value (optional)
Function_Name <- function(arg1, arg2, ...) 
{ 
   Function body 
   Return value
}

Examples

The following code script R07.Functions.R provides some examples of using functions:

RTutorial.plus <- function(a,b)
{
  result <- a+b
  print(paste('result is ', result))
  return(result)
}

RTutorial.plus(10,11)

RTutorial.plus <- function(a,b)
{
  result <- a+b
  print(paste('result is ', result))
}

result<-RTutorial.plus(10,11)

print(result)

RTutorial.plus <- function(a,b=11)
{
  result <- a+b
  print(paste('result is ', result))
  return(result)
}

RTutorial.plus(10)
It shows example functions with/without return values. The last function also has an optional parameter named 'b' with default value '11'
Sample output
> RTutorial.plus <- function(a,b)
+ {
+   result <- a+b
+   print(paste('result is ', result))
+   return(result)
+ }
> 
> RTutorial.plus(10,11)
[1] "result is  21"
[1] 21
> 
> RTutorial.plus <- function(a,b)
+ {
+   result <- a+b
+   print(paste('result is ', result))
+ }
> 
> result<-RTutorial.plus(10,11)
[1] "result is  21"
> 
> print(result)
[1] "result is  21"
> 
> RTutorial.plus <- function(a,b=11)
+ {
+   result <- a+b
+   print(paste('result is ', result))
+   return(result)
+ }
> 
> RTutorial.plus(10)
[1] "result is  21"
[1] 21

Summary

Hopefully you now have a good understanding about the basics of R programming. In the other articles of this series, I will show more advanced topics about R programming. 

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts