R is a programming language and software environment for statistical analysis, graphics representation, and reporting.

Local Environment Setup:

  • Download R from the following link: http://cran.us.r-project.org/.
  • Click on Download R for (Mac) OS X.Click on Download R-3.5.1.pkg
  • Install R. Leave all default settings in the installation options.
  • Download RStudio Desktop for Mac from the RStudio Website.

R Command Prompt:
Once you have R environment setup, then it’s easy to start your R command prompt by just typing the following command at your command prompt −

$ R

Now you can use install command at R prompt to install the required package. For example, the following command will install plotrix package which is required for 3D charts.

> install.packages("plotrix")

Hello World Program:

As a convention, we will start learning R programming by writing a “Hello, World!” program. Depending on the needs, you can program either at R command prompt or you can use an R script file to write your program. Let’s check both one by one.

> myString <- "Hello, World!" 
> print ( myString) 
[1] "Hello, World!"

Here, the first statement defines a string variable myString, where we assign a string “Hello, World!” and then next statement print() is being used to print the value stored in variable myString.

R Script File:

Usually, you will do your programming by writing your programs in script files and then you execute those scripts at your command prompt with the help of R interpreter called Rscript. So let’s start with writing following code in a text file called test.R as under −

# My first program in R Programming
myString <- "Hello, World!"

print ( myString)

Save the above code in a file test.R and execute it at Linux command prompt as given below. Even if you are using Windows or other system, syntax will remain same.

$ Rscript test.R 

When we run the above program, it produces the following result.

[1] "Hello, World!"

Comments:

Comments are like helping text in your R program and they are ignored by the interpreter while executing your actual program. Single comment is written using # in the beginning of the statement as follows −

# My first program in R Programming

R does not support multi-line comments but you can perform a trick which is something as follows −

if(FALSE) {
   "This is a demo for multi-line comments and it should be put inside either a 
      single OR double quote"
}

myString <- "Hello, World!"
print ( myString)

Though above comments will be executed by R interpreter, they will not interfere with your actual program. You should put such comments inside, either single or double quote.

R – Data Types

In R, the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable. There are many types of R-objects. The frequently used ones are −

  • Vectors
  • Lists
  • Matrices
  • Arrays
  • Factors
  • Data Frames

The simplest of these objects is the vector object and there are six data types of these atomic vectors, also termed as six classes of vectors. The other R-Objects are built upon the atomic vectors.

Data TypeExampleVerify
LogicalTRUE, FALSEv <- TRUE
print(class(v))
it produces the following result −
[1] “logical”
Numeric12.3, 5, 999 v <- 23.5
print(class(v))
it produces the following result −
[1] “numeric”
Integer2L, 34L, 0Lv <- 2L
print(class(v))
it produces the following result −
[1] “integer”
Complex3 + 2iv <- 2+5i
print(class(v))
it produces the following result −
[1] “complex”
Character‘a’ , ‘”good”, “TRUE”, ‘23.4’v <- “TRUE”
print(class(v))
it produces the following result −
[1] “character”
Raw“Hello” is stored as 48 65 6c 6c 6fv <- charToRaw(“Hello”) print(class(v))
it produces the following result −
[1] “raw”

Vectors:
When you want to create vector with more than one element, you should use c() function which means to combine the elements into a vector.

# Create a vector.
apple <- c('red','green',"yellow")
print(apple)

# Get the class of the vector.
print(class(apple))

When we execute the above code, it produces the following result −

[1] "red"    "green"  "yellow"
[1] "character"

Vector considers all the elements of same data type.

> vec1 = c("a", "def", 1, 2.5, 3+4i)

> vec1
# Output
[1] "a"    "def"  "1"    "2.5"  "3+4i"

> class(vec1)
# Output
[1] "character"

Lists

A list is an R-object that can contain many different types of elements inside it like vectors, functions, and even another list inside it.

# Create a list.
list1 <- list(c(2,5,3),21.3,sin)

# Print the list.
print(list1)

When we execute the above code, it produces the following result −

[[1]]
[1] 2 5 3

[[2]]
[1] 21.3

[[3]]
function (x)  .Primitive("sin")

Matrices:

# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)

# Output
    [,1] [,2] [,3] 
[1,] "a" "a" "b" 
[2,] "c" "b" "a"

Arrays

While matrices are confined to two dimensions, arrays can be of any number of dimensions. The array function takes a dim attribute which creates the required number of dimension. In the below example we create an array with two elements which are 3×3 matrices each.

# Create an array.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)

When we execute the above code, it produces the following result −

, , 1

     [,1]     [,2]     [,3]    
[1,] "green"  "yellow" "green" 
[2,] "yellow" "green"  "yellow"
[3,] "green"  "yellow" "green" 

, , 2

     [,1]     [,2]     [,3]    
[1,] "yellow" "green"  "yellow"
[2,] "green"  "yellow" "green" 
[3,] "yellow" "green"  "yellow"  

Factors

Factors are the r-objects which are created using a vector. It stores the vector along with the distinct values of the elements in the vector as labels. The labels are always character irrespective of whether it is numeric or character or Boolean etc. in the input vector. They are useful in statistical modeling.

Factors are created using the factor() function. The nlevels functions gives the count of levels.

# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')

# Create a factor object.
factor_apple <- factor(apple_colors)

# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))

When we execute the above code, it produces the following result −

[1] green  green  yellow red    red    red    green 
Levels: green red yellow
[1] 3

Data Frames

Data frames are tabular data objects. Unlike a matrix in data frame each column can contain different modes of data. The first column can be numeric while the second column can be character and third column can be logical. It is a list of vectors of equal length.

Data Frames are created using the data.frame() function.

# Create the data frame.
BMI <- 	data.frame(
   gender = c("Male", "Male","Female"), 
   height = c(152, 171.5, 165), 
   weight = c(81,93, 78),
   Age = c(42,38,26)
)
print(BMI)

When we execute the above code, it produces the following result −

  gender height weight Age
1   Male  152.0     81  42
2   Male  171.5     93  38
3 Female  165.0     78  26 

Variables:

A variable provides us with named storage that our programs can manipulate. A variable in R can store an atomic vector, group of atomic vectors or a combination of many Robjects.

The variables can be assigned values using leftward, rightward and equal to operator. The values of the variables can be printed using print() or cat() function. The cat() function combines multiple items into a continuous print output.

# Assignment using equal operator. 
var.1 = c(0,1,2,3) # Assignment using leftward operator. 
var.2 <- c("learn","R") # Assignment using rightward operator.
c(TRUE,1) -> var.3 

print(var.1) 
cat ("var.1 is ", var.1 ,"\n") 
cat ("var.2 is ", var.2 ,"\n") 
cat ("var.3 is ", var.3 ,"\n")

When we execute the above code, it produces the following result −
Note − The vector c(TRUE,1) has a mix of logical and numeric class. So logical class is coerced to numeric class making TRUE as 1.

[1] 0 1 2 3 
var.1 is 0 1 2 3 
var.2 is learn R 
var.3 is 1 1

Deleting Variables:
Variables can be deleted by using the rm() function. Below we delete the variable var.3. On printing the value of the variable error is thrown.

rm(var.3) 
print(var.3)

When we execute the above code, it produces the following result −

[1] "var.3" 
Error in print(var.3) : object 'var.3' not found