Basic Syntax, Data Types and Variables
By convention, we will start coding R using “Hello World” program. This can be done using the direct R programming using the command prompt or writing a program in a file as R script and executing it.
R command Prompt
With the help of command prompt,
Let’s print “Hello World”
string is a variable name used to store “hello” and when print(string) is written, it outputs: hello as shown above
R Script File
Executing scripts at the R command prompt using R interpreter is called Rscript. These scripts are the programs written in a file which runs on the command prompt.
Prior to execution R Script in the command prompt, PATH has to be added to environment variables in advanced system properties as shown below:
Write the below code in a notepad++ or in the R Script console and save it as testing.R
# Sample example code
sss <- “Welcome to R”
Although it is tested in windows, when using Linux or other OS, the syntax remains same.
When we run the program $ Rscript testing.R , it generates output as  “Welcome to R”
These are the words to be ignored by the interpreter while executing the program. Single line commenting can be done using # at the beginning of the program line
#My 1st R program
R does not support multi-line comments, but can be done using a trick as follows:
‘This is an example for multi-line comment statements within single or double quote’
str <- “Welcome to R!”
print ( str)
Although the comment will get executed, it doesn’t interfere with the actual program and prints output as Hello R.
While doing programming, you will need to create a variable which will consume some memory location to store values. Based on the data type, the OS will allow you to allocates memory and decides values to store in the reserved memory.
Unlike other programming languages such as C and Java, In R, the variables are assigned with R-Objects and variables are not declared as some data type. Some of the popular R-objects are:
- Data Frames
The less complicated one among these is the vector object and there are six data types of them, also called as six classes of vectors.
13.2, 0, 678
4L, 33L, 0L
‘84.3’, “z”, “board”
produces the output as
“World” is allocated as 39 43 3b 3c 3f
produces the output as
In this language, the basic data types are the R-objects called vectors which clasp elements of various classes as shown above.
When more than one vector needs to be created, you should use c() function which combines the elements into a vector.
#example for creating a vector
intellipaat <- c(‘amateur’, ‘pro’, ‘expert’)
#To know the class of the vector
The output for the above code will be:
 “amateur” “pro” “expert”
Object which have vectors, functions and other lists too.
#Creating the list
listx <- list(20,c(‘amateur’, ‘pro’, ‘expert’), san)
#Printing the list
 amateur pro expert
function (x) .Primitive(“san”)
It is a 2-D rectangular data set. It can be formed by vector representation to the matrix function output.
#creating a matrix
M= matrix(c(‘a’,’b’,’c’,’d’,’e’,’f’), nrow = 2, ncol = 3, byrow = TRUE)
The Output will be:
[,1] [,2] [,3]
[1,] “a” “b” “c”
[2,] “d” “e” “f”
While we already studied about the 2-D concept of matrices; But unlike matrices, arrays has no limit in dimensions. In the below example, we have taken an array with two elements which are 3×3 matrices each.
#creating an array
arr <- array(c(‘blue’,’red’), dim = c(3,3,2)
[,1] [,2] [,3]
[1,] “blue” “red” “blue”
[2,] “red” “blue” “red”
[3,] “blue” “red” “blue”
[,1] [,2] [,3]
[1,] “red” “blue” “red”
[2,] “blue” “red” “blue”
[3,] “red” “blue” “red”
Factors are the r-objects which are generated using a vector. It stores the vector along with the distinct values of the elements in the vector as labels(these are always character type)
It has an attribute as factor() function. The count of levels is given by nlevels functions.
#Creating a vector
mango_colours <- c(‘yellow’, ‘yellow’, ‘green’, ‘red’, ‘red’, ‘red’, ‘yellow’)
#creating a factor object
factor_mango <- factor(mango_colors)
#printing the factor
 yellow yellow green red red red yellow
Levels: yellow red green
#applying nlevels function to know distinct values
Data frames are tabular data objects. Compared to the matrix, Data Frames can contain different modes of data in each column. It can contain numeric values in the first column, character in the second and logical values over the third column. It is a list of vectors of equal length.
Data Frames are generated using the data.frame() function.
#creating the data frame
df <- data.frame(
name = c(“sac”, “san”, “pal”),
ht = c(153, 172, 167),
wt = c(44, 66, 77),
age = c(22, 33, 44)
name ht wt age
1 sac 153 44 22
2 san 172 66 33
3 pal 167 77 44
A variable in R can store a group of vectors, an atomic vector or a combination of many R objects. A list of valid variable name is shown below:
var_nam_2, .var_name, var.name are valid variables.
Variables having character %, starts with number or dot or _ are invalid.
Using leftward, rightward and equal operator, the values of the variables can be printed using cat() or print()function. The cat() function combines multiple things into a continuous print output.
#Assignment via equal operator
var.x = c(0,1,2,3)
#Assignment via leftward operator
var.y <- c(“use”, “R Programming”)
#Assignment via rightward operator
c(TRUE,1) -> var.z
cat(“var.x is”, var.x, “\n”)
cat(“var.y is”, var.y, “\n”)
cat(“var.z is”, var.z, “\n”)
 0 1 2 3
var.x is 0 1 2 3
var.y is use R Programming
var.z is 1 1
Data Type of a Variable
In R, there is no pre-occupied data type for a variable, instead the variable gets a datatype of the assigned R- object. Hence R is dynamically typed language. This implies that we can change the variable’s data type when using in a program.
variable_l <- ” welcome”
cat(“Class of variable_l is”, class(variable_l),”\n”)
variable_l <- 27.1
cat(“Now class of variable_l is”, class(variable_l), “\n”)
variable_l <- 33L
cat(” Next the class of variable_l becomes”, class(variable_l), “\n”)
The class of variable_l is character
Now class of variable_l is numeric
Next class of variable_l becomes integer
To check all the variables in the workspace, we use the ls() function. Also ls() function can use patterns to match the variable names i.e ls(pattern = “var”)
Also if you want variables starting with dot() can be listed using “all.names= TRUE”. This lists all the variables.
Variables could easily be deleted using rm() function. After that, when we print the value of variable, an error is thrown.
Error in print(var.x) : object ‘var.x’ not found
All the variables can also be deleted at once using,