R Programming Variables Datatypes Vectors Lists Notes
R Programming Variables Datatypes Vectors Lists Notes
Bengaluru, Karnataka
Computer Science & Engineering
School of Computer Science & Engineering
R variables:
Introduction to Variables:
1. In R, variables are used to store data values.
2. They are crucial for performing calculations, data analysis, and storing information in
your R programs.
Variable Assignment:
You can assign values to variables using the assignment operator <- or =.
For example: x <- 10 or y = "Hello"
Data Types:
R supports various data types for variables, including numeric, character, logical, factor, and
more.
è Use the class() function to check the data type of a variable.
Naming Rules:
1. Variable names should start with a letter (a-z or A-Z) or a dot (.) followed by letters,
numbers, or underscores.
2. Avoid using reserved words like if, else, function, etc., as variable names.
Printing Variables:
To display the value of a variable, simply type the variable's name.
For example: x will display the value of the variable x.
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 1
Data Coercion:
R can automatically convert data types when needed. This is called data coercion.
For example, if you add a character and a numeric value, R will convert the character to a
numeric value if possible.
Vectors:
In R, you often work with vectors, which are one-dimensional collections of data.
You can create a vector using the c() function. For example: numbers <- c(1, 2, 3, 4, 5)
Factor Variables:
Factors are used to represent categorical data in R.
You can create a factor variable using the factor() function.
Missing Values:
R uses NA to represent missing or undefined values in variables.
You can check for missing values using functions like is.na().
Variable Scope:
Variables in R have a scope, which determines where they can be accessed.
Global variables are accessible throughout the entire R session, while local variables are
confined to specific functions or blocks.
Variable Manipulation:
You can perform various operations on variables, such as arithmetic calculations, data
transformation, and statistical analysis.
Variable Functions:
R provides many built-in functions for working with variables, like mean(), sum(), length(),
etc., to perform calculations and manipulate data.
Variable Attributes:
Variables can have attributes like names, dimensions, and data types. You can access and
manipulate these attributes using functions like attributes(), dim(), and names().
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 2
Data Frames:
Data frames are special types of variables that store data in a tabular format.
They are commonly used for data analysis and manipulation in R.
Constants:
In R, you can create constants by assigning a value to a variable and not changing it during the
program's execution.
R Data types:
The concept of data type is crucial in programming.
Different forms of data can be stored in variables, and different types can perform various
functions.
In R, variables can even alter their type after they have been set and do not need to be defined
with a specific type:
Example:
variable_1=25
variable_2<-35
print(variable_2)
print(variable_1)
Output:
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 3
Example:
# numeric
a <- 12.3
print(class(a))
# integer
b <- 10L
print(class(b))
# complex
c <- 2i + 9
print(class(c))
# character/string
print(class(x))
# logical
x <- TRUE
print(class(x))
Output
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 4
1. Number:
Numeric – 10.2
Integer – 10L
Complex – 2+i
Note: Variables of number types are created when you assign a value to them
Type Conversion:
You can convert from one type to another with the following functions:
• as.numeric()
• as.integer()
• as.complex()
Example:
y <- 4 # numeric
print(class(x))
print(class(y))
a <- as.numeric(x)
b <- as.integer(y)
print(x)
print(y)
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 5
# print the class name of a and b
print(class(a))
print(class(b))
Output:
Example:
print(max(10,20,30))
print(min(10,20,30))
print(sqrt(49))
print(abs(-4))
print(abs(-5.3))
print(floor(3.4))
print(ceiling(3.4))
Output:
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 6
String inbuilt functions:
Example:
string="Hello World"
print(nchar(string))
example 1:
string="Hello World"
print(grepl("H",string))
Example 2:
string="Hello World"
print(grepl("h",string))
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 7
Here, the executable statements in the body of the loop is a series of operations computed for
each item in the sequence, which is a collection of objects (such as a vector) through which the
for-loop iterates.
Example 1:
for(x in 10)
{
print(x)
}
Example 2:
for(x in 0:5)
{
print(x)
}
Example 3:
for(x in 1:5)
{
print(x)
}
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 8
Example 4:
for(days in 1:7)
{
print(paste("Day:",days))
}
R Data Structures:
Vectors:
A collection of identically categorised elements is all that makes up a vector. Utilise the c()
function and a comma to divide the list of items into a vector. We build a vector variable called
fruits in the example below, which combines the following strings:
Example 1:
a=c(1,2,3,4)
print(a)
print(class(a))
for(i in a)
{
print(i)
}
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 9
Example 2:
names=c("Rohini","Srabana","Meena","Sreedevi")
for(i in names)
{
print(i)
}
Example 3:
numbers=1:20
print(numbers)
Example 4:
numbers=1.3:20.3
print(numbers)
Example 5:
values=c(TRUE, FALSE)
print(values)
Example 6:
values=c(TRUE, FALSE)
print(length(values))
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 10
Sort a Vector:
To sort vector elements is alphabetical order use sort() method.
Example:
num=c(20,10,40,12,34)
print(sort(num))
Example 2:
num=c(20,10,40,12,34)
print(num[-1:0])
print(num[2:4])
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 11
To edit values:
Example:
num=c(20,10,40,12,34)
num[2]=100
print(num)
Repeat Vectors:
Example:
repeat_vector = rep(c(10,20,30), each = 2)
print(repeat_vector)
Sequencing in vectors:
Example:
num=seq(from=1,to=100,by=30 )
print(num)
Vectors:
A list of objects of the same type is all that a vector is.
Use the c() method and a comma between each item in the list to combine it into a vector.
Example1:
number=c(10,20,30)
print(number)
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 12
Example 2:
number=c(10.2,20,30)
print(number)
Example 3:
ch=c("Abc","cde")
print(ch)
Use the following operator to build a vector with numerical values in a sequential order:
Example 1:
numbers=1:100
print(numbers)
Example 2:
numbers=1.2:4.3
print(numbers)
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 13
Example 3:
To print Logical values
input=c(TRUE,FALSE)
print(input)
Example 4:
To print the length
numbers=c(10,20,30,40,50)
print(length(numbers))
Sorting of Vectors:
names=c("rohini", "bavesh","Mac")
numbers=c(12,2,13,23,1)
print(sort(names))
print(sort(numbers))
Output:
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 14
Example 2:
names=c("a","bc","cs")
print(names[c(2,3)])
Example 3:
names=c("a","bc","cs")
print(names[-2])
Example 4:
numbers=seq(from=10, to=100, by=30)
print(numbers)
Lists:
In R, a list can hold a wide variety of data types. A list is an organised, modifiable collection
of data.
Utilise the list() method to generate a list:
Example:
values=list(1,"abc","TRUE")
print(values)
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 15
Output:
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 16
Example: To change values
values=list(1,"abc","TRUE")
values[2]=2
print(values)
Example:
thislist <- list("apple", "banana", "cherry")
print(thislist[2:3])
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 17
Output:
Joining 2 lists:
Example:
numbers=list(1,2,"a","b",3)
n=list(10,20,30)
list3=c(numbers,n)
print(list3)
Output:
Prepared by,
Dr.Srabana Pramanik, AP/SoCSE, Ms. Rohini A, AP/SoCSE 18