0% found this document useful (0 votes)
179 views2 pages

R Programming for Beginners

This document contains examples of R code demonstrating various R basics concepts including: 1) Defining functions to calculate elapsed time between dates in days, handle NAs in a vector by removing odd numbers and NAs, and convert strings to dates 2) Using functions and loops to calculate the sum of the first N numbers, output classmarks based on conditional logic, and calculate the hypotenuse of a right triangle 3) Demonstrating use of functions by passing arguments and handling edge cases like non-numeric inputs or negative sides of a triangle.

Uploaded by

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

R Programming for Beginners

This document contains examples of R code demonstrating various R basics concepts including: 1) Defining functions to calculate elapsed time between dates in days, handle NAs in a vector by removing odd numbers and NAs, and convert strings to dates 2) Using functions and loops to calculate the sum of the first N numbers, output classmarks based on conditional logic, and calculate the hypotenuse of a right triangle 3) Demonstrating use of functions by passing arguments and handling edge cases like non-numeric inputs or negative sides of a triangle.

Uploaded by

Akshay Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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

************R Basics-String and Date************


convert_date<-function(S){
ans<-as.Date(S,"%d%m%Y")
return(ans)
}

print(convert_date("15081947"))
print(convert_date("18081995"))
print(convert_date("04021967"))
print(convert_date("06091969"))

************R Basics-Iteration-2************
sum_whole <- function(N) {
sum(seq_len(N))
}

print(sum_whole(20))
print(sum_whole(32))
print(sum_whole(4))

************R Basics-Conditional Operators-2************


classmark <- function(marks) {
ans <- c()
for (i in 1:length(marks)) {
if (marks[i] > 90) {
ans <- "Best Class"
} else {
ans <- "Needs Improvement"
}
}
return(ans)
}
print(classmark(c(100,95,94,56)))
print(classmark(c(100,95,94,96)))

************R Basics-User Defined Function************


pyth <- function(height, base)
{
sides <- c(height, base)
if(any(sides < 0))
{
message("sides must be positive")
} else if(!is.numeric(x = sides))
{
message("sides can not be non-numeric")
} else
{
sqrt(x = sum(sides ^ 2))
}
}
print(pyth(3,4))
print(pyth(6,8))

You might also like