PRACTICAL=1
R DATA TYPES
AIM= data types
1.logical
v<-TRUE
> print(class(v))
[1] "logical"
>
2.Numeric
> v<-22.5
> print(class(v))
[1] "numeric"
>
3.Integer
> v<-2L
> print(class(v)
[1] "integer"
> v<-2+5i
4.complex
> print(class(v))
[1] "complex"
> v<-"true"
> print(class(v))
5.create a vector
> apple<-c('red','green','yellow')
> print(apple)
[1] "red" "green" "yellow"
> print(class(apple))
[1] "character"
6.LIST
> list1<-list(c(2,5,3),21.3,sin)
> print(list 1)
Error: unexpected numeric constant in "print(list 1"
> print(list1)
[[1]]
[1] 2 5 3
[[2]]
[1] 21.3
[[3]]
function (x) .Primitive("sin")
7.MATRIX
> M=matrix(c('a','a','b','c','b','a'))
> print(M)
[,1]
[1,] "a"
[2,] "a"
[3,] "b"
[4,] "c"
[5,] "b"
[6,] "a"
> M=matrix(c('a','a','b','c','b','a'),nrow=2,ncol=3,byrow=TRUE)
> print(M)
[,1] [,2] [,3]
[1,] "a" "a" "b"
8.Array
<-array(c('green','yellow'),dim=c(3,3,2))
> print(a)
,,1
[,1] [,2] [,3]
[1,] "green" "yellow" "green"
[2,] "yellow" "green" "yellow"
[3,] "green" "yellow" "green"
[,1] [,2] [,3]
[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"
9.create factor
factor_apple<-factor(apple_colors)
> print(factor_apple)
[1] green green yellow red red red green
Levels: green red yellow
10.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,3
8,26))
> print(BMI)
gender height weight age
1 male 152.0 81 42
2 male 171.5 93 38
3 female 165.0 78 26
Practical 2.
> var.1=c(0,1,2,3)
> var.2=c('learn','R')
> c(TRUE,1)->var.3
> print(var.1)
[1] 0 1 2 3
cat("var.1 is",var.1,"\n")
var.1 is 0 1 2 3
cat("var.2 is",var.2,"\n")
var.2 is learn R
> cat("var.3 is",var.3,"\n")
var.3 is 1 1
var_x<-"hello"
> cat("the class of var_x is",class (var_x),"\n")
the class of var_x is character
> var_x<-34.5
> cat("the class of var_x is",class (var_x),"\n")
the class of var_x is numeric
> var_x<-27L
> cat("the class of var_x is",class (var_x),"\n")
the class of var_x is integer
R-OPERATORS
ADDITION OF VECTORS
> v<-c(2,5.5,6)
> t<-c(8,3,4)
> print(v+t)
>SUBTRACTION OF TWO VECTORS
>v<-c(2,5.5,6)
> t<-c(8,3,4)
> print(v-t)
[1] -6.0 2.5 2.0
MULTIPLICATION OF TWO VECTORS
v<-c(2,5.5,6)
> t<-c(8,3,4)
> print(v*t)
[1] 16.0 16.5 24.0
DIVISION
> <-c(2,5.5,6)
> t<-c(8,3,4)
> print(v/t)
[1] 0.250000 1.833333 1.500000
%% VECTOR
v<-c(2,5.5,6)
> t<-c(8,3,4)
> print(v%%t)
[1] 2.0 2.5 2.0
> t<-c(8,3,4)
> v<-c(2,5.5,6)
> print(t^v)
[1] 64.0000 420.8883 4096.0000
Relational operator
Greater than
> v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v>t)
[1] FALSE TRUE FALSE FALSE
Less than
v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v<t)
[1] TRUE FALSE TRUE FALSE
Equal to
v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v==t)
[1] FALSE FALSE FALSE TRUE
Less than equal to
> v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v<=t)
[1] TRUE FALSE TRUE TRUE
Greater than equal to
> v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v>=t)
[1] FALSE TRUE FALSE TRUE
Unequal
> v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v!=t)
[1] TRUE TRUE TRUE FALSE
Practical 4.
R if…else
In computer programming, the if statement allows us to create a decision
making program.
A decision making program runs one block of code under a condition and
another block of code under different conditions. For example,
If age is greater than 18, allow the person to vote.
If age is not greater than18, don’t allow the person to vote.
R if Statement
The syntax of if statement is:-
if (test_expression) {
#body of if
Example: -
> x<-3
> if(x>0){print("The number is positive")}
[1] "The number is positive"
R if…else Statement
We can also use an optional else statement with if statement. The syntax of an
if…else statement is:-
if(test_expression) {
#body of if statement
}else {#body of else statement}
Example:- 1
Input:-
> age<-15
> if (age>18) {
print('you are eligible to vote')
else{
print('you are not eligible to vote')
Output:
[1] "you are not eligible to vote"
Example:- 2
input:-
> age<-19
> if (age>18) {print('you are eligible to vote')} else{print('you are not eligible to
vote')}
Output:-
[1] "you are eligible to vote"
Example: - 3
Input:-
> no<- 2
> if(no>0){print("The no. is positve")} else{print("The no. is negative")}
Output:-
[1] "The no. is positve"
Example: - 4
Input:-
> no<- -2
> if(no>0){print("The no. is positve")} else{print("The no. is negative")}
Output:-
[1] "The no. is negative"
R if…else if…else Statement
If you want to test more than one condition, you can choose the optional else
if statement along with your if…else statements
SYNTAX:-
if(test_expression1) {
#code block 1
} else if (test_expression2){
#code block 2
} else {
3code block 3
Example: -
Input 1:-
> x<-0
> if (x>0){print("The number is positive")} else if(x<0){print("The number is
negative")} else{print("x is zero")}
Output:-
[1] "x is zero"
Input 2:-
> x<-2
> if (x>0){print("The number is positive")} else if(x<0){print("The number is
negative")} else{print("x is zero")}
Output:-
[1] "The number is positive"
Input 3:-
> x<- -3
> if (x>0){print("The number is positive")} else if(x<0){print("The number is
negative")} else{print("x is zero")}
Output:-
[1] "The number is negative"
Nested if…else Statement
Input 1:-
> x<-20
> if(x>0)
{if(x%%2==0){
print("x is positive even number")}
else{print("x is positive odd number")}
else {if(x%%2==0){
print("x is negative even number")}
else{print("x is negative odd number")}}
Output:
[1] "x is positive even number"
Input 2:
> x<- -2
> if(x>0)
{if(x%%2==0){
print("x is positive even number")}
else{print("x is positive odd number")}
else {if(x%%2==0){
print("x is negative even number")}
else{print("x is negative odd number")}}
Output:
[1] "x is negative even number"
Practical 5.
And operator(&)
Logical operators
It is applicable only to vectors of type logical ,numeric or complex.
v<-c(3,1,TRUE,2+3i)
> t<-c(4,1,FALSE,2+3i)
> print(v&t)
[1] TRUE TRUE FALSE TRUE
Not operator
v<-c(3,0,TRUE,2+3i)
> print(!v)
[1] FALSE TRUE FALSE FALSE
AND(&&)operator
t<-c(2+2i)
> v<-c(2+2i)
> print(t&&v)
[1] TRUE
OR(||)operator
v<-c(2+2i)
> t<-c(2+2i)
> print(v||t)
[1] TRUE
ASSIGNMENT OPERATOR
These operators used to assign values to vectors.
LEFT ASSIGNMENT
v1<-c(3,1,TRUE,2+3i)
> v2<-c(3,1,TRUE,2+3i)
> v3<-c(3,1,TRUE,2+3i)
> print(v1)
[1] 3+0i 1+0i 1+0i 2+3i
> print(v2)
[1] 2+2i
> print(v3)
[1] 3+0i 1+0i 1+0i 2+3i
RIGHT ASSIGNMENT
c(3,1,TRUE,2+3i)->v1
> c(3,1,TRUE,2+3i)->>v2
> print(v1)
[1] 3+0i 1+0i 1+0i 2+3i
> print(v2)
[1] 3+0i 1+0i 1+0i 2+3i
Miscellaneous operator
Colon(:)
v<-2:8
> print(v)
[1] 2 3 4 5 6 7 8
%in%
v1<-8
> v2<-12
> t<-1:10
> print(v1%in%t)
[1] TRUE
> print(v2%in%t)
[1] FALSE
(%*%)
M=matrix(c(2,6,5,1,10,4),nrow=2,ncol=3,byrow=TRUE)
> t=M%*%t(M)
> print(t)
[,1] [,2]
[1,] 65 82
[2,] 82 117
IF ELSE FUNCTION FOR ODD\EVEN NUMBERS
EXAMPLE 1
#input vector
> x<-c(2,9,23,14,20,1,5)
> #if else()function to determine odd even numbers
> ifelse(x%%2==0,"EVEN","ODD")
[1] "EVEN" "ODD" "ODD" "EVEN" "EVEN" "ODD" "ODD"
EXAMPLE2
marks<-c(63,58,12,99,49,39,41,2)
#If else ()function to determine pass/fail
> ifelse(marks<40,"FAIL","PASS")
[1] "PASS" "PASS" "FAIL" "PASS" "PASS" "FAIL" "PASS" "FAIL"
Practical 6.
: R -Switch Statement
A Switch statement allows a variable to be tested for a equality against a list of values.
Each value is called case ,and the variable being switched on is checked for each case.
Syntax :
Switch (expression,case1,case2,case3…….)
Example 1:
val<-switch(4,"Greek1","Greek2","Greek3","Greek4","Greek5","Greek6")
> print(val)
[1] "Greek4"
Example 2:
val1=6
> val2=7
> val3="s"
> result=switch(
+ val3,
+ "a"=cat("addition=",val1+val2),
+ "d"=cat("subtraction=",val1-val2),
+ "r"=cat("division=",val1/val2),
+ "s"=cat("multiplication=",val1*val2),
+ "m"=cat("modulus=",val1%%val2),
+ "p"=cat("power=",val1^val2)
+)
multiplication= 42
> print(result)
NULL
Example 3:
> x<-switch(
+ 3,
+ "first",
+ "second",
+ "third",
+ "fourth",
> print(x)
[1] "third"
> x<-switch(
+ 4,
+ "first",
+ "second",
+ "third",
+ "fourth",
+)
> print(x)
[1] "fourth"
x<-switch(
+ 3,
+ "first",
+ "second")
> print(x)
NULL
EXAMPLE 4;
ax=1
> bx=2
> y=switch(
+ ax+bx,
+ "hello shubham",
+ "hello arokita",
+ "hello vaishali",
+ "hello nishika"
+)
> print(y)
[1] "hello vaishali"
ax=3
> bx=3
> y=switch(
+ ax+bx,
+ "hello shubham",
+ "hello nishika",
+ "hello arpita",
+)
> print(y)
NULL
Example 5:
Month ="janvier"
> season=switch(
+ month,
+ "january"="winter",
+ "february"="winter",
+ "march"="winter",
+ "april"="spring",
+ "may"="summer",
+ "june"="summer",
+ "july"="rainy",
+ "august"="rainy",
+ "september"="rainy",
+ "october"="rainy",
+ "november"="winter",
+ "december"="winter"
+)
> print(season)
[1] "winter"
Example 6:
Day=3
> weekday=switch(
+ Day,
+ "sunday",
+ "monday",
+ "tuesday",
+ "wednesday",
+ "thursday",
+ "friday",
+ "saturaday",
+)
> print(weekday)
[1] "tuesday"
Example 7:
Y=”18”
> x=switch(
+ y,
+ "9"="hello arpita",
+ "12"="hello vaishali",
+ "18"="hello nishika",
+ "21"="hello shubham",
+)
> print(x)
[1] "hello nishika"
Example 8:
> y="18"
> a=10
> b=2
> x=switch(
+ y,
+ "9"=cat("addition=",a+b),
+ "12"=cat("subtraction=",a-b),
+ "18"=cat("division=",a/b),
+ "21"=cat("multiplication=",a*b),
+)
division= 5
> print(x)
NULL
Practical 7.
Example 1:
v<-LETTERS[1:4]
> for (i in v ){
+ print(i)
+}
[1] "A"
[1] "B"
[1] "C"
[1] "D"
Example 2:
> for(x in 1:5){
+ print(x)
+}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
for(month in 1:5){
+ print(paste('month','month'))
+}
[1] "month month"
[1] "month month"
[1] "month month"
[1] "month month"
[1] "month month"
> for(month in 1:5){
+ if(month<3){
+ print(paste("winter","month",'month'))
+ }else{
+ print(paste("spring","month",'month'))
+}
+}
[1] "winter month month"
[1] "winter month month"
[1] "spring month month"
[1] "spring month month"
[1] "spring month month"
> for(month in 1:5){