R Basics Hands On
R Basics Hands On
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')
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
M<-10:20
print(M)
which(M>13 & M<16)
Random Numbers in R
sh is
V<-round(runif(10,0,1), 2)
print(V )
ifelse(V<0.50,"Less","More")
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)
\
POSIX Date
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
factorial <- 1
if(n==0 | n==1){
factorial <- 1
} else{
while(n >= 1){
ed d
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 your code here
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 your code here
emp�<-�data.frame(
���empno�=�11,
���emp_name�=�"Alex",
���age�=�32,
���dept�="sales"
)
str(emp)
Conditional Operations-2
#Write your code here
toppers<-sample(80:100,�10)
print(toppers)
ifelse(toppers>90,"best class","needs improvements")
for�(val�in�toppers)�{
m
�if(val>90){
er as
��print("best class")
co
�}else
eH w
�{
���print("needs improvements")
o.
�} rs e
}
ou urc
User Defined Functions
o
#Write your code here
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
print(a�)
Th
for() Loops in R
#Write your code here
X=80:100
tot<-0
for�(val�in�X)�{
��if(val%%2==0){
tot<-tot+val
��}
}
print(tot)
Vectors in R
#Write your code here
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)
Handling 'NA'
#Write your code here
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 your code here
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 your code here
o.
A<-11:16 rs e
B<-13:20
ou urc
setdiff(A,B)
Descriptive Statistics in R
#Write your code here
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)