Create a list using R with the following values g1=1:10, g2=“R Programming”, g3= “HTML”. Then, count the number of objects in the list. After that, get the length of the first two vectors of the given list.
mylist<- list(g1=c(1:10), g2="R Programming", g3="HTML")
print(mylist)
## $g1
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $g2
## [1] "R Programming"
##
## $g3
## [1] "HTML"
length(mylist)
## [1] 3
length(mylist[[1]])
## [1] 10
length(mylist[[2]])
## [1] 1
Find all elements of a list1 that are not given in list2 using R. Given list1 consists of “x”, “y”, “z” and list2 consists of “x”, “y”, “z”, “X”, “Y”, “Z”. (You can use the setdiff())
list1<-list(c("x", "y", "z"))
list2<-list(c("x", "y", "z", "X", "Y", "Z"))
setdiff(list1[[1]], list2[[1]])
## character(0)
Create a list using the following values (“G022”, “G003”, “G013”, “G007”, “G012” and then list down the number of items in the list. Then, display the second element in the list. After that, add a new item “G018”to the list and then display the list in ascending order.
mylist<- list("G022", "G003", "G013", "G007", "G012")
mylist
## [[1]]
## [1] "G022"
##
## [[2]]
## [1] "G003"
##
## [[3]]
## [1] "G013"
##
## [[4]]
## [1] "G007"
##
## [[5]]
## [1] "G012"
mylist1<- append(mylist, "G018")
mylist1
## [[1]]
## [1] "G022"
##
## [[2]]
## [1] "G003"
##
## [[3]]
## [1] "G013"
##
## [[4]]
## [1] "G007"
##
## [[5]]
## [1] "G012"
##
## [[6]]
## [1] "G018"
mylist2<-as.list(sort(unlist(mylist1)))
mylist2
## [[1]]
## [1] "G003"
##
## [[2]]
## [1] "G007"
##
## [[3]]
## [1] "G012"
##
## [[4]]
## [1] "G013"
##
## [[5]]
## [1] "G018"
##
## [[6]]
## [1] "G022"
Create four vectors a, b, c, and d with 3 integers. Combine all the vectors to become a 4x3 matrix. Add a vector with sequence of number from 1 to 4 to the matrix by row.
a<- c(1, 2, 3)
b<- c(4, 5, 6)
c<- c(7, 8, 9)
d<- c(10, 11, 12)
matrix1<- matrix(c(a, b, c, d), 4, 3, byrow = TRUE)
e<- sequence(4, 1:4)
matrix1<-rbind(t(matrix1), e)
print(matrix1)
## [,1] [,2] [,3] [,4]
## 1 4 7 10
## 2 5 8 11
## 3 6 9 12
## e 1 2 3 4
A and B is a 3x4 matrix. Create an R Script to multiply matrix A and B to get the 3x3 dimension.
A<- matrix(sample(1:10, 12, replace = TRUE), 3, 4)
B<- matrix(sample(1:10, 12, replace = TRUE), 3, 4)
C<- A%*%t(B)
print(C)
## [,1] [,2] [,3]
## [1,] 187 142 156
## [2,] 219 105 199
## [3,] 190 131 135
Create a matrix with 10 rows and 2 columns, extract a sub-matrix from original matrix which includes the last 5 rows.
matrix1<- matrix(sample(1:10, 20, replace = TRUE), 10, 2)
matrix1
## [,1] [,2]
## [1,] 7 9
## [2,] 1 4
## [3,] 10 3
## [4,] 9 10
## [5,] 6 5
## [6,] 7 10
## [7,] 7 9
## [8,] 1 3
## [9,] 10 4
## [10,] 7 2
matrix1[6:10,]
## [,1] [,2]
## [1,] 7 10
## [2,] 7 9
## [3,] 1 3
## [4,] 10 4
## [5,] 7 2
A square matrix A is said to be invertible if there exists a matrix B such that AB = BA = I. Generate a 2x2 matrix A as below. Then, compute B.
A<- matrix(c(2, 2, 4, 2), 2, 2, byrow = TRUE)
print(B<-solve(A))
## [,1] [,2]
## [1,] -0.5 0.5
## [2,] 1.0 -0.5
With regards to the mtcars dataset:
a.retrieve the number of columns
b.retrieve the number of rows
c.retrieve data value from row 3 and column 5
d.retrieve data value from row 3 and column 5 using the names
e.retrieve data of a row (Volvo 142E)
f.retrieve data of column mpg
g.retrieve data of column hp and qsec
#a
cat("columns=", columns<- ncol(mtcars))
## columns= 11
#b
cat("rows=", rows<- nrow(mtcars))
## rows= 32
#c
print(mtcars[3, 5])
## [1] 3.85
#d
print(mtcars["Datsun 710", "drat"])
## [1] 3.85
#e
print(mtcars["Volvo 142E",])
## mpg cyl disp hp drat wt qsec vs am gear carb
## Volvo 142E 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2
#f
print(mtcars["mpg"])
## mpg
## Mazda RX4 21.0
## Mazda RX4 Wag 21.0
## Datsun 710 22.8
## Hornet 4 Drive 21.4
## Hornet Sportabout 18.7
## Valiant 18.1
## Duster 360 14.3
## Merc 240D 24.4
## Merc 230 22.8
## Merc 280 19.2
## Merc 280C 17.8
## Merc 450SE 16.4
## Merc 450SL 17.3
## Merc 450SLC 15.2
## Cadillac Fleetwood 10.4
## Lincoln Continental 10.4
## Chrysler Imperial 14.7
## Fiat 128 32.4
## Honda Civic 30.4
## Toyota Corolla 33.9
## Toyota Corona 21.5
## Dodge Challenger 15.5
## AMC Javelin 15.2
## Camaro Z28 13.3
## Pontiac Firebird 19.2
## Fiat X1-9 27.3
## Porsche 914-2 26.0
## Lotus Europa 30.4
## Ford Pantera L 15.8
## Ferrari Dino 19.7
## Maserati Bora 15.0
## Volvo 142E 21.4
#g
print(mtcars[c("mpg", "qsec")])
## mpg qsec
## Mazda RX4 21.0 16.46
## Mazda RX4 Wag 21.0 17.02
## Datsun 710 22.8 18.61
## Hornet 4 Drive 21.4 19.44
## Hornet Sportabout 18.7 17.02
## Valiant 18.1 20.22
## Duster 360 14.3 15.84
## Merc 240D 24.4 20.00
## Merc 230 22.8 22.90
## Merc 280 19.2 18.30
## Merc 280C 17.8 18.90
## Merc 450SE 16.4 17.40
## Merc 450SL 17.3 17.60
## Merc 450SLC 15.2 18.00
## Cadillac Fleetwood 10.4 17.98
## Lincoln Continental 10.4 17.82
## Chrysler Imperial 14.7 17.42
## Fiat 128 32.4 19.47
## Honda Civic 30.4 18.52
## Toyota Corolla 33.9 19.90
## Toyota Corona 21.5 20.01
## Dodge Challenger 15.5 16.87
## AMC Javelin 15.2 17.30
## Camaro Z28 13.3 15.41
## Pontiac Firebird 19.2 17.05
## Fiat X1-9 27.3 18.90
## Porsche 914-2 26.0 16.70
## Lotus Europa 30.4 16.90
## Ford Pantera L 15.8 14.50
## Ferrari Dino 19.7 15.50
## Maserati Bora 15.0 14.60
## Volvo 142E 21.4 18.60
a.Display the information above in a table.
b.Display the information above in a table sort by product name in ascending order.
c.Add a new product SK023, Johnson PH, 5.99 to the data frame and display the information in a table sort by product name in ascending order.
d.Display all rows where product price more than 8.00.
e.Display the product with maximum price and minimum price.
f.Count the number of items where the product name begins with “A”
df<- data.frame(
ProductCode=c("SK020", "SK042", "SK013", "SK066", "SK079"),
ProductName=c("Enfagrow A+", "Ayamas", "100 Plus", "Ali Cafe", "Dettol Natural"),
Price=c(36.79, 9.99, 6.49, 8.99, 4.99)
)
#a
df
## ProductCode ProductName Price
## 1 SK020 Enfagrow A+ 36.79
## 2 SK042 Ayamas 9.99
## 3 SK013 100 Plus 6.49
## 4 SK066 Ali Cafe 8.99
## 5 SK079 Dettol Natural 4.99
#b
df_b<-df[order(df$ProductName),]
print(df_b)
## ProductCode ProductName Price
## 3 SK013 100 Plus 6.49
## 4 SK066 Ali Cafe 8.99
## 2 SK042 Ayamas 9.99
## 5 SK079 Dettol Natural 4.99
## 1 SK020 Enfagrow A+ 36.79
#c
new_row<- list("SK023", "Johnson PH", 5.99)
df<-rbind(df, new_row)
df_c<-df[order(df$ProductName),]
print(df_c)
## ProductCode ProductName Price
## 3 SK013 100 Plus 6.49
## 4 SK066 Ali Cafe 8.99
## 2 SK042 Ayamas 9.99
## 5 SK079 Dettol Natural 4.99
## 1 SK020 Enfagrow A+ 36.79
## 6 SK023 Johnson PH 5.99
#d
df_e<- subset(df, Price>8)
print(df_e)
## ProductCode ProductName Price
## 1 SK020 Enfagrow A+ 36.79
## 2 SK042 Ayamas 9.99
## 4 SK066 Ali Cafe 8.99
#e
df_max<- df[which.max(df$Price),]
print(df_max)
## ProductCode ProductName Price
## 1 SK020 Enfagrow A+ 36.79
df_min<- df[which.min(df$Price),]
print(df_min)
## ProductCode ProductName Price
## 5 SK079 Dettol Natural 4.99
#f
df_f <- subset(df, substr(ProductName,1,1) =="A")
print(df_f)
## ProductCode ProductName Price
## 2 SK042 Ayamas 9.99
## 4 SK066 Ali Cafe 8.99
print(nrow(df_f))
## [1] 2