0% found this document useful (0 votes)
120 views

R Programming Hands-On

1) The document shows various R code examples for performing operations like adding vectors, conditional operations using if/else and which, converting dates and times, using for loops, variable operations including type conversion and random numbers, vector and matrix operations, factors and levels, POSIX dates, and handling NA values. 2) Functions are defined to demonstrate conditional operations, date conversions, random number generation, variable operations like type conversion, set operations, vector operations, and handling missing values. 3) The code examples are run and the results are printed to demonstrate how to perform various tasks in R like arithmetic on vectors, conditional logic, dates and times, loops, random numbers, and handling missing data.

Uploaded by

Akshay Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

R Programming Hands-On

1) The document shows various R code examples for performing operations like adding vectors, conditional operations using if/else and which, converting dates and times, using for loops, variable operations including type conversion and random numbers, vector and matrix operations, factors and levels, POSIX dates, and handling NA values. 2) Functions are defined to demonstrate conditional operations, date conversions, random number generation, variable operations like type conversion, set operations, vector operations, and handling missing values. 3) The code examples are run and the results are printed to demonstrate how to perform various tasks in R like arithmetic on vectors, conditional logic, dates and times, loops, random numbers, and handling missing data.

Uploaded by

Akshay Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

************R Basics-3 - Vectors in R************

v1<-c(1,2,3,4,5)
v2<-c(10,20,30,40,50)
v1+v2

******R Basics-10- coding- Conditional Operations- if/else*******


conditional <- function(V)
{
Answer <- ifelse(V>=80,"Above_Average","Average")
cat(Answer)
}

conditional(c(78,85,90))

******R Basics- coding- Conditional Operations- which*******


conditional <- function(M)
{

# Enter your code here. Read input from STDIN. Print output to STDOUT
ans<-which(M>13 & M<16)

return(ans)
}
print(conditional( c(10:20)))

******R Basics- coding- Date and Time in R*******


str1 = "25-12-2016"
date1 = as.Date(str1,"%d-%m-%Y")
print(date1)

******R Basics- coding- for() Loops in R*******

X<-80:100
sum<-0
Y=X[which(X %% 2 == 0)]
for(i in Y) {sum= sum+i}
print(sum)

******R Basics- coding-Variables*******


leap<-function(D){

ans<-format(D,format="%Y")

return (ans)
}
print(leap(as.Date('2018-01-01')))
print(leap(as.Date('2022-01-01')))

******R Basics- coding-Type Conversion*******


convert<-function(K){

is.integer(K)
ans<-as.integer(K)

return (ans)
}
print(convert(2.7))
print(convert(3.456))
print(convert(4.1))

******R Basics- coding-Random Numbers*******


random_function <- function(length,start,end,precision) {
V <- runif(10,0,1)
V <- round(V, digits=2)

return (V)

}
print(length(random_function(10,0.0,10.0,2)))

******R Basics- coding-Variable Operations*******


operation<-function(L1,L2){
ans<-sqrt(L1)*L2

return (ans)
}
print(operation(4,2))
print(operation(100,4))

******R Basics- coding-Set Operations*******


set_handling <- function(A,B)
{
C<-setdiff(A, B)

return (C)
}
print(set_handling(c(11:16),c(13:20)))

******R Basics- coding-Vector Operations*******


multi<-function(a,b,c,d){
v1<-seq(a,b)
v2<-seq(c,d)
ans<-v1*v2

return(ans)
}
print(multi(1,3,4,6))
print(multi(10,16,24,30))

******R Basics- coding-Vector and Matrices*******


V<-1:9
M<-matrix( V,nrow=3,byrow=TRUE)
M<-M*2
M

******R Basics- coding-Factors and Levels*******


V<-factor(c("yes","no","yes","maybe"))
levels(V)

******R Basics- coding-Which()*******


find_matches<-function(scores){

# Enter your code here. Read input from STDIN. Print output to STDOUT
ans<-which(scores%%2==0)
return(ans)
}
print(find_matches(c(102,34,56,77)))
print(find_matches(c(100,90,21)))

******R Basics- coding-*******

*****R Basics-22 - POSIX Date*****


elapsed_days<-function(X,Y){
ans<-as.Date(X, "%d%b%Y") - as.Date(Y, "%d%b%Y")

return (ans)
}
print(elapsed_days("15Jan2020","12Dec1983"))
print(elapsed_days("18Aug2021","18Aug1995"))

*****R Basics-Handling 'NA'*****


handling_na <- function(V)
{
V<-vec
V<-V[!(is.na(V) | V%%2==1)]
return (V)
}
vec<-c(1, 4 ,NA ,7 ,9 ,NA ,2)
print(handling_na(vec))

You might also like