0% found this document useful (0 votes)
13 views15 pages

Krushang

The document is a lesson plan on data types and operators in R from Marwadi University. It discusses the different data types in R including logical, numeric, integer, complex, character and raw. It also covers the various operators in R such as arithmetic, relational, logical, assignment and miscellaneous operators. The lesson includes examples of code demonstrating each data type and operator. It provides pre-lab questions and programming problems for students to practice working with data types and operators in R.

Uploaded by

muchhalabhavin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views15 pages

Krushang

The document is a lesson plan on data types and operators in R from Marwadi University. It discusses the different data types in R including logical, numeric, integer, complex, character and raw. It also covers the various operators in R such as arithmetic, relational, logical, assignment and miscellaneous operators. The lesson includes examples of code demonstrating each data type and operator. It provides pre-lab questions and programming problems for students to practice working with data types and operators in R.

Uploaded by

muchhalabhavin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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

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

## Error in eval(expr, envir, enclos): object 'Y' not found


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

There are five operators in R:

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

x<- 3 or x = 3 (Leftwards Assignment)

3 -> x or x = 3 (Rightwards Assignment)

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:

1. List and define some basic data types in R.

________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
__________________________________________________

2. What is use of operator in R?

________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
__________________________________________________

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

1. Logical Data type

#logical Datatype

v<-TRUE

print(class(v))

print(v)

2. Numeric Data type

#Numeric Datatype

v<-23.5

print(class(v))

print(v)

3. Integer Data type

#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)

4. Complex Data type

#Complex Datatype

v<-2+5i

print(class(v))

print(v)

5. Character Data type

#Character

v="true"

print(class(v))

print(v)

6. Raw Data type

#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

Write R script that demonstrates the functionality of all the operators of

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.

speed = as.numeric(readline("enter the speed"))

time = as.numeric(readline("enter the time"))

distance = speed*time

print(distance)

OUTPUT:

2. Body Mass Index (BMI) Calculation : Write a program to calculate BMI using weight and height
inputs.

weight = as.numeric(readline("enter the weight"))

height = as.numeric(readline("enter the height"))


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

a=height*height

BMI = (weight)/a

cat("BMI = ",BMI)

OUTPUT:

3. Temperature Conversion : Develop a program to convert temperature from Celsius to Fahrenheit


and vice versa.

x=as.integer(readline("enter the Calsius"))

fer = (x*9/5)+32

y=as.integer(readline("enter the ferenhit"))

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.

x=as.numeric(readline("enter the miles"))

y=as.numeric(readline("enter the fuel fill his tank"))

a=x/y

b=a*4.544

c=a/b

cat("Miles per Gallon",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

radius = as.numeric(readline("enter the radius"))

height = as.numeric(readline("enter the height"))

a= 3.14*radius*radius*height

cat("volume of cylinder = ",a)

OUTPUT:

You might also like