r programming docx
r programming docx
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)
> print(class(apple))
[1] "character"
6.LIST
> list1<-list(c(2,5,3),21.3,sin)
> print(list 1)
> print(list1)
[[1]]
[1] 2 5 3
[[2]]
[1] 21.3
[[3]]
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)
8.Array
<-array(c('green','yellow'),dim=c(3,3,2))
> print(a)
,,1
9.create factor
factor_apple<-factor(apple_colors)
> print(factor_apple)
> print(BMI)
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
var.3 is 1 1
var_x<-"hello"
> var_x<-34.5
R-OPERATORS
ADDITION OF VECTORS
> v<-c(2,5.5,6)
> t<-c(8,3,4)
> print(v+t)
> t<-c(8,3,4)
> print(v-t)
> t<-c(8,3,4)
> print(v*t)
DIVISION
> <-c(2,5.5,6)
> t<-c(8,3,4)
> print(v/t)
%% VECTOR
v<-c(2,5.5,6)
> t<-c(8,3,4)
> print(v%%t)
> t<-c(8,3,4)
> v<-c(2,5.5,6)
> print(t^v)
Relational operator
Greater than
> v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v>t)
Less than
v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v<t)
Equal to
v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v==t)
> t<-c(8,2.5,14,9)
> print(v<=t)
> t<-c(8,2.5,14,9)
> print(v>=t)
Unequal
> v<-c(2,5.5,6,9)
> t<-c(8,2.5,14,9)
> print(v!=t)
Practical 4.
R if…else
A decision making program runs one block of code under a condition and
another block of code under different conditions. For example,
R if Statement
#body of if
Example: -
> x<-3
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
Example:- 1
Input:-
> age<-15
> if (age>18) {
else{
Output:
input:-
> age<-19
> if (age>18) {print('you are eligible to vote')} else{print('you are not eligible to
vote')}
Output:-
Example: - 3
Input:-
> no<- 2
Output:-
Example: - 4
Input:-
> no<- -2
Output:-
If you want to test more than one condition, you can choose the optional else
SYNTAX:-
if(test_expression1) {
#code block 1
} else if (test_expression2){
#code block 2
} else {
3code block 3
Example: -
Input 1:-
> x<-0
Output:-
Input 2:-
> x<-2
Output:-
[1] "The number is positive"
Input 3:-
> x<- -3
Output:-
Input 1:-
> x<-20
> if(x>0)
{if(x%%2==0){
else {if(x%%2==0){
Output:
> x<- -2
> if(x>0)
{if(x%%2==0){
else {if(x%%2==0){
Output:
Practical 5.
And operator(&)
Logical operators
> t<-c(4,1,FALSE,2+3i)
> print(v&t)
Not operator
v<-c(3,0,TRUE,2+3i)
> print(!v)
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
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)
> print(v2)
[1] 2+2i
> print(v3)
RIGHT ASSIGNMENT
c(3,1,TRUE,2+3i)->v1
> c(3,1,TRUE,2+3i)->>v2
> print(v1)
> print(v2)
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)
> ifelse(x%%2==0,"EVEN","ODD")
EXAMPLE2
marks<-c(63,58,12,99,49,39,41,2)
> ifelse(marks<40,"FAIL","PASS")
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)
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)
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:
+ print(x)
+}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
for(month in 1:5){
+ print(paste('month','month'))
+}
+ if(month<3){
+ print(paste("winter","month",'month'))
+ }else{
+ print(paste("spring","month",'month'))
+}
+}