Krushang
Krushang
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
Aim:To understand various data types in R and working with different operators
IDE:R Studio
Theory:
In programming languages, we need to use various variables to store various information. Variables
are the reserved memory location to store values. As we create a variable in our program, some space
is reserved in memory.
In R, there are several data types such as integer, string, etc. The operating system allocates memory
based on the data type of the variable and decides what can be stored in the reserved memory.
Data types in R
To make the best of the R language, you'll need a strong understanding of the basic data types and
data structures and how to operate on those.
• character
• numeric (real or decimal)
• integer
• logical
• complex
Case Sensitivity
Lastly, note that R is a case sensitive programming language. Meaning all variables, functions, and
objects must be called by their exact spelling:
x <- 1
y <- 3
z <- 4
x * y * z ## [1] 12
x*Y*z
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Miscellaneous Operators
1. Arithmetic Operators
These operators perform basic arithmetic operations like addition, subtraction, multiplication,
division, exponent, modulus, etc.
2. Relational Operators
These operators are used to compare two values or variables. To find if one is smaller, greater,
equal, not equal, and other similar operations these operators are used. A relational operator is
always a Logical value, that is either TRUE or FALSE.
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
3. Logical Operators
These operators are used to perform Boolean operations like AND, OR, NOT, etc. on variables.
Different logical operators are as follows:
4. Assignment Operators
The use of these operators is to assign values to the variables. There are two kinds of assignments,
leftwards assignment, and rightwards assignment.Operators ‘<-‘ and ‘=’ are used to assign values to
any variable.
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
5. Miscellaneous Operators
These R programming operators are used for special cases and are not for general mathematical or
logical computation.colon operator – It is used to generate a series of numbers in sequence for a vector.
Pre Lab:
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
__________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
__________________________________________________
3. What is difference between Integer data type and numeric data type?
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
__________________________________________________
Programs:
Write R script that demonstrates the functionality of all the Data type
#logical Datatype
v<-TRUE
print(class(v))
print(v)
#Numeric Datatype
v<-23.5
print(class(v))
print(v)
#Integer Datatype
v<-2L
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
print(class(v))
print(v)
#Complex Datatype
v<-2+5i
print(class(v))
print(v)
#Character
v="true"
print(class(v))
print(v)
#Raw
v<-charToRaw("Krushang")
print(class(v))
print(v)
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
1. Arithmetic Operators
#Arithmetic Operator
v<-c(2,5.5,6)
t<-c(8,3,4)
print(v+t)
print(v-t)
print(v*t)
print(v/t)
print(v%%t)
print(v%/%t)
print(v^t)
2. Relational Operators
#Relational operator
v<-c(2,5.5,6,9)
t<-c(8,2.5,14,9)
print(v>t)
print(v<t)
print(v==t)
print(v<=t)
print(v>=t)
print(v!=t)
3. Logical Operators
#Logical operator
v<-c(3,1,TRUE,2+3i)
t<-c(4,1,FALSE,2+3i)
print(v&t)
print(v|t)
print(!v)
v=101
t=121
print(v&&t)
print(v||t)
print(v!=t)
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
4. Assignment Operators
#Assignment operator
v1<-c(3,1,TRUE,2+3i)
v2<<-c(3,1,TRUE,2+3i)
v3=c(3,1,TRUE,2+3i)
print(v1)
print(v2)
print(v3)
c(3,1,TRUE,2+3i)->v1
c(3,1,TRUE,2+3i)->>v2
print(v1)
print(v2)
5. Miscellaneous Operators
#Miscellaneous operator
v<-2:8
print(v)
v1<-8
v2<-12
t<-1:10
print(v1 %in% t)
print(v2 %in% t)
Output:
OUTPUT 1:
OUTPUT 2:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
OUTPUT 3:
OUTPUT 4:
OUTPUT 5:
OUTPUT6:
OUTPUT 1:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
OUTPUT 2:
OUTPUT 3:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
OUTPUT 4:
OUTPUT 5:
Observation :
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
Conclusion:
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
________________________________________________________________________________
________________________________________________________________________________
Post Lab:(Write code for each program and mention output screenshot here)
1. Distance Calculation : Develop a program to calculate the distance traveled by a vehicle given its
speed and time.
distance = speed*time
print(distance)
OUTPUT:
2. Body Mass Index (BMI) Calculation : Write a program to calculate BMI using weight and height
inputs.
a=height*height
BMI = (weight)/a
cat("BMI = ",BMI)
OUTPUT:
fer = (x*9/5)+32
cal = (x-32)*5/9
print(fer)
print(cal)
OUTPUT:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
4. Fuel Efficiency Calculation : Develop a program to calculate fuel efficiency (miles per gallon) of a
vehicle given the distance traveled and fuel consumed.
a=x/y
b=a*4.544
c=a/b
cat("\nFuel Consumed",c)
OUTPUT:
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:To understand various data types in R and working with different
and R Studio(01CT0106) operators
Experiment: 03 Date: Enrollment No:92301733060
5. Volume of a Cylinder: Create a program to calculate the volume of a cylinder given its radius and
height.
#volume of cylinder
a= 3.14*radius*radius*height
OUTPUT: