x<-3L
class(x)
## [1] "integer"
y<-as.numeric(x)
y
## [1] 3
class(y)
## [1] "numeric"
typeof(x)
## [1] "integer"
typeof(y)
## [1] "double"
max(3,4,5)
## [1] 5
sqrt(16)
## [1] 4
ceiling(1.4)
## [1] 2
floor(1.4)
## [1] 1
str <- "Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."
str
## [1] "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt\nut labore et dolore magna aliqua."
nchar(str)
## [1] 123
grepl("e",str)
## [1] TRUE
str<-"Hello world!"
str2<-"This is"
nchar(str)
## [1] 12
a<-paste(str2,str)
a
## [1] "This is Hello world!"
a<-200
b<-33
if (b>a){
print("B")
}else{
print(a)
}
## [1] 200
x=5
y=2
z=x%%y
z
## [1] 1
z2=x%/%y
z2
## [1] 2
#Nested If Statements
x<-41
if(x>10){
print("Above 10")
if(x>20){
print("and also above 20")
}else{
print("but less than 20")
}
}else{
print("below 10")
}
## [1] "Above 10"
## [1] "and also above 20"
a <- 200
b <- 33
c <- 500
if (a > b & c > a){
print("Both conditions are true")
}
## [1] "Both conditions are true"
i <- 1
while (i < 6) {
print(i)
i <- i + 1
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
i <- 1
while (i < 6) {
print(i)
i <- i + 1
if (i == 4) {
break
}
}
## [1] 1
## [1] 2
## [1] 3
i <- 0
while (i < 6) {
i <- i + 1
if (i == 3) {
next
}
print(i)
}
## [1] 1
## [1] 2
## [1] 4
## [1] 5
## [1] 6
i<-0
while (i<6){
i<-i+1
if(i==3){
next
}
print(i)
}
## [1] 1
## [1] 2
## [1] 4
## [1] 5
## [1] 6
for (x in 1:10){
print(x)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
dice<-1:6
for(x in dice){
if(x==6){
print("Yahtzee!")
}else{
print(paste(x,"Not Yahtzee"))
}
}
## [1] "1 Not Yahtzee"
## [1] "2 Not Yahtzee"
## [1] "3 Not Yahtzee"
## [1] "4 Not Yahtzee"
## [1] "5 Not Yahtzee"
## [1] "Yahtzee!"
adj <- list("red", "big", "tasty")
fruits <- list("apple", "banana", "cherry")
for (x in adj) {
for (y in fruits) {
print(paste(x, y))
}
}
## [1] "red apple"
## [1] "red banana"
## [1] "red cherry"
## [1] "big apple"
## [1] "big banana"
## [1] "big cherry"
## [1] "tasty apple"
## [1] "tasty banana"
## [1] "tasty cherry"
my_function<-function(fname,lname="Poghos"){
paste(fname,lname)
}
my_function("Art")
## [1] "Art Poghos"
m_f<-function(x){
return(5*x)
}
m_f(4)
## [1] 20
Nested_function <- function(x, y) {
a <- x + y
return(a)
}
Nested_function(Nested_function(2,2), Nested_function(3,3))
## [1] 10
Outer_func <- function(x) {
Inner_func <- function(y) {
a <- x + y
return(a)
}
return (Inner_func)
}
output <- Outer_func(3) # To call the Outer_func
output(5)
## [1] 8
tri_recursion <- function(k) {
if (k > 0) {
result <- k + tri_recursion(k - 1)
print(result)
} else {
result = 0
return(0)
}
}
tri_recursion(6)
## [1] 1
## [1] 3
## [1] 6
## [1] 10
## [1] 15
## [1] 21
n=c(1:10)
length(n)
## [1] 10
d=c(5,3,7,9,2)
sort(d)
## [1] 2 3 5 7 9
d[1]
## [1] 5
y=sort(d)
y[1]
## [1] 2
y[c(1,3)]
## [1] 2 5
d[c(-1)]
## [1] 3 7 9 2
d[1]<-100
d
## [1] 100 3 7 9 2
y_repeat<-rep(y,each=3)
y_repeat
## [1] 2 2 2 3 3 3 5 5 5 7 7 7 9 9 9
y_repeat1<-rep(y,times=3)
y_repeat1
## [1] 2 3 5 7 9 2 3 5 7 9 2 3 5 7 9
y_repeat2<-rep(y,times=c(2,3,4,5,6))
y_repeat2
## [1] 2 2 3 3 3 5 5 5 5 7 7 7 7 7 9 9 9 9 9 9
numbers<-1:10
numbers
## [1] 1 2 3 4 5 6 7 8 9 10
numbers<-seq(1:10)
numbers
## [1] 1 2 3 4 5 6 7 8 9 10
numbers<-seq(from=0,to=20,by=2)
numbers
## [1] 0 2 4 6 8 10 12 14 16 18 20
n=c(2,3,"nn")
n
## [1] "2" "3" "nn"
s<-list("apple",2,3)
length(s)
## [1] 3
"aple" %in% s
## [1] FALSE
dd=append(s,"aple",after=2)
"aple" %in% dd
## [1] TRUE
dd2<-s[-1]
for (x in dd){
print (x)
}
## [1] "apple"
## [1] 2
## [1] "aple"
## [1] 3
ss=seq(from=2,to=30,by=2)
ss
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
attributes(ss)
## NULL
class(ss)
## [1] "numeric"
typeof(ss)
## [1] "double"
dim(ss)<-c(3,5)
ss
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2 8 14 20 26
## [2,] 4 10 16 22 28
## [3,] 6 12 18 24 30
dim(ss)<-c(1,3,5)
ss
## , , 1
##
## [,1] [,2] [,3]
## [1,] 2 4 6
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 8 10 12
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 14 16 18
##
## , , 4
##
## [,1] [,2] [,3]
## [1,] 20 22 24
##
## , , 5
##
## [,1] [,2] [,3]
## [1,] 26 28 30
s1<-seq(from=2,to=30,by=2)
s1
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
dim(s1)<-c(3,5)
s1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2 8 14 20 26
## [2,] 4 10 16 22 28
## [3,] 6 12 18 24 30
typeof(s1)
## [1] "double"
class(s1)
## [1] "matrix" "array"
my_m<-matrix(c(1,2,3,4,5,6),nrow = 3,ncol = 2)
my_m
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
thismatrix <- matrix(c(2, "banana", "cherry", "orange"), nrow = 2, ncol = 2)
thismatrix
## [,1] [,2]
## [1,] "2" "cherry"
## [2,] "banana" "orange"
thismatrix[1,2]
## [1] "cherry"
thismatrix[2,]
## [1] "banana" "orange"
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix
## [,1] [,2] [,3]
## [1,] "apple" "orange" "pear"
## [2,] "banana" "grape" "melon"
## [3,] "cherry" "pineapple" "fig"
thismatrix[c(1,2),]
## [,1] [,2] [,3]
## [1,] "apple" "orange" "pear"
## [2,] "banana" "grape" "melon"
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
newmatrix <- cbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
#newmatrix <- rbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
# Print the new matrix
newmatrix
## [,1] [,2] [,3] [,4]
## [1,] "apple" "orange" "pear" "strawberry"
## [2,] "banana" "grape" "melon" "blueberry"
## [3,] "cherry" "pineapple" "fig" "raspberry"
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "mango", "pineapple"), nrow = 3, ncol =2)
#Remove the first row and the first column
thismatrix <- thismatrix[-c(1), -c(1)]
thismatrix
## [1] "mango" "pineapple"
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
"apple" %in% thismatrix
## [1] TRUE
length(thismatrix)
## [1] 4
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
thismatrix
## [,1] [,2]
## [1,] "apple" "cherry"
## [2,] "banana" "orange"
for(rows in 1:nrow(thismatrix)){
for(columns in 1:ncol(thismatrix)){
print(thismatrix[rows,columns])
}
}
## [1] "apple"
## [1] "cherry"
## [1] "banana"
## [1] "orange"
# Combine matrices
Matrix1 <- matrix(c("apple", "banana", "cherry", "grape"), nrow = 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango", "pineapple", "watermelon"), nrow = 2, ncol = 2)
# Adding it as a rows
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined
## [,1] [,2]
## [1,] "apple" "cherry"
## [2,] "banana" "grape"
## [3,] "orange" "pineapple"
## [4,] "mango" "watermelon"
# Adding it as a columns
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined
## [,1] [,2] [,3] [,4]
## [1,] "apple" "cherry" "orange" "pineapple"
## [2,] "banana" "grape" "mango" "watermelon"