0% found this document useful (2 votes)
16K views4 pages

R Basics Hands On

This document contains code snippets demonstrating various R programming concepts like variables, data types, conditional operations, functions, loops, vectors, matrices, dates, factors and descriptive statistics. The code shows how to define and manipulate variables, write if/else conditional statements, create and call functions, perform for loops, work with vectors, matrices, dates and factors, and calculate descriptive statistics.

Uploaded by

murugan
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 (2 votes)
16K views4 pages

R Basics Hands On

This document contains code snippets demonstrating various R programming concepts like variables, data types, conditional operations, functions, loops, vectors, matrices, dates, factors and descriptive statistics. The code shows how to define and manipulate variables, write if/else conditional statements, create and call functions, perform for loops, work with vectors, matrices, dates and factors, and calculate descriptive statistics.

Uploaded by

murugan
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/ 4

#Write your code here

type conversation

K<-2

if (class(K)!="integer") {

K <- as.integer(K)
print(K)
}

R Basics - Variables
#Write your code here

L<-as.Date('2018-01-01')

get_next_leap_year <- function(L) {


all_years <- L + 1:4
all_years[(all_years %% 4 == 0 & all_years %%100 != 0) | all_years %% 400 ==
0]

m
}

er as
co
LN <-get_next_leap_year(2018)

eH w
print(LN)

o.
Conditional Operations - if/else
rs e
ou urc
#Write your code here

Marks <- c(78,85,90)


o
aC s
v i y re

for (value in Marks) {


if(value>=80){print("Above Average")}else{print("Average")}
}

Conditional Operations - which()


ed d

#Write your code here


ar stu

M<-10:20
print(M)
which(M>13 & M<16)

Random Numbers in R
sh is

#Write your code here


Th

V<-round(runif(10,0,1), 2)
print(V )
ifelse(V<0.50,"Less","More")

Factors and Levels

#Write your code here


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

Vector Operations
#Write your code here

X<-1:5
Y<-seq(2,10,2)

This study source was downloaded by 100000790328255 from CourseHero.com on 06-28-2021 15:07:07 GMT -05:00

https://www.coursehero.com/file/73662899/R-Basics-hands-ontxt/
print(X*Y)

Matrix Operations
#Write your code here
#Write your code here
MM <- matrix(c(2,4,6,8,0,9),nrow = 2)
print(MM)
\

String and Date


a <- '15081947'
a <- as.Date(a,"%d%B%Y")

POSIX Date

#Write your code here

test1 <- as.Date("15Aug1947", format="%d%b%Y")

m
test2 <- strptime("15Aug2018", format="%d%b%Y")

er as
difftime(as.POSIXct(test2), as.POSIXct(test1), units="days")

co
eH w
Conditional Operators-1
#Write your code here

o.
temp<-c(103,100,98) rs e
ifelse(temp>100,"hot","good")
ou urc
Iterations 1

#Write your code here


o
aC s

findfactorial <- function(n){


v i y re

factorial <- 1
if(n==0 | n==1){
factorial <- 1
} else{
while(n >= 1){
ed d

factorial <- factorial * n


ar stu

n <- n-1
}
}
return (factorial)
}
sh is

findfactorial(6)
Th

Iterations
#Write your code here
x <- 1:20
count <- 0
for (val in x) {
count = count+val
}
print(count)

which()

#Write&nbspyour&nbspcode&nbsphere
M<-c(98,102,120,145,175,169,118,177,101,200)
print(M)
which(M%%2==0)

This study source was downloaded by 100000790328255 from CourseHero.com on 06-28-2021 15:07:07 GMT -05:00

https://www.coursehero.com/file/73662899/R-Basics-hands-ontxt/
str()
#Write&nbspyour&nbspcode&nbsphere

emp�<-�data.frame(
���empno�=�11,
���emp_name�=�"Alex",
���age�=�32,

���dept�="sales"
)
str(emp)

Conditional Operations-2
#Write&nbspyour&nbspcode&nbsphere

toppers<-sample(80:100,�10)
print(toppers)
ifelse(toppers>90,"best&nbspclass","needs&nbspimprovements")

for�(val�in�toppers)�{

m
�if(val>90){

er as
��print("best&nbspclass")

co
�}else

eH w
�{
���print("needs&nbspimprovements")

o.
�} rs e
}
ou urc
User Defined Functions
o

#Write&nbspyour&nbspcode&nbsphere
aC s

pyth�<-�function(a,�b){
v i y re

hypotenuse�<-�sqrt(a^2�+�b^2)
return(hypotenuse)
}
pyth(3,4)
ed d
ar stu

Date and Time in R


#Write&nbspyour&nbspcode&nbsphere
a�<-�'25-12-2016'
a�<-�as.Date(a,"%d-%m-%Y")
sh is

print(a�)
Th

for() Loops in R
#Write&nbspyour&nbspcode&nbsphere
X=80:100
tot<-0
for�(val�in�X)�{
��if(val%%2==0){
tot<-tot+val
��}
}
print(tot)

Vectors in R
#Write&nbspyour&nbspcode&nbsphere

V1<-1:5

This study source was downloaded by 100000790328255 from CourseHero.com on 06-28-2021 15:07:07 GMT -05:00

https://www.coursehero.com/file/73662899/R-Basics-hands-ontxt/
V2<-c(10,20,30,40,50)
print(V1+V2)

Vectors and Matrices


#Write&nbspyour&nbspcode&nbsphere
V<-1:9
M�<-�matrix(V,�nrow�=�3,�byrow�=�TRUE)
print(M*2)

Handling 'NA'
#Write&nbspyour&nbspcode&nbsphere
V<-c(1,4,NA,7,9,NA,2)
x<-V[!is.na(V)&�(V�%%�2�==�0)]
print(x)
Categorical to Numeric Variable in R
#Write&nbspyour&nbspcode&nbsphere

v1�<-�factor(sample(�10))
print(v1)
v2<-as.numeric(v1)

m
print(v2)

er as
v3<-v1==v2

co
print(v3)

eH w
�Set Operations in R
#Write&nbspyour&nbspcode&nbsphere

o.
A<-11:16 rs e
B<-13:20
ou urc
setdiff(A,B)
Descriptive Statistics in R
#Write&nbspyour&nbspcode&nbsphere
A<-11:17
o

median(A)
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000790328255 from CourseHero.com on 06-28-2021 15:07:07 GMT -05:00

https://www.coursehero.com/file/73662899/R-Basics-hands-ontxt/
Powered by TCPDF (www.tcpdf.org)

You might also like