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.
alist <- list(g1=c(1:10), g2="R Programming", g3= "HTML")
print(alist)
## $g1
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $g2
## [1] "R Programming"
##
## $g3
## [1] "HTML"
print(length(alist))
## [1] 3
cat("alist[[1]] =", length(alist[[1]]))
## alist[[1]] = 10
# cat("alist[[2]] =", nchar(alist[[2]]))
cat("alist[[2]] =", length(alist[[2]]))
## alist[[2]] = 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("x", "y", "z")
list2 <- list("x", "y", "z", "X", "Y", "Z")
diff <- setdiff(list2, list1)
print(diff)
## [[1]]
## [1] "X"
##
## [[2]]
## [1] "Y"
##
## [[3]]
## [1] "Z"
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.
Q3 <- list("G022", "G003", "G013", "G007", "G012")
print(length(Q3))
## [1] 5
print(Q3[[2]])
## [1] "G003"
Q3 <- append(Q3,"G018")
Q3a <- (as.list(sort(unlist(Q3))))
print(Q3a)
## [[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 <- sample(1:10, 3)
cat("a =", a)
## a = 4 2 10
b <- sample(1:10, 3)
cat("b =", b)
## b = 5 7 8
c <- sample(1:10, 3)
cat("c =", c)
## c = 7 1 6
d <- sample(1:10, 3)
cat("d =", d)
## d = 7 9 10
matrix1 <- matrix(c(a,b,c,d),4,3, byrow=TRUE)
print(matrix1)
## [,1] [,2] [,3]
## [1,] 4 2 10
## [2,] 5 7 8
## [3,] 7 1 6
## [4,] 7 9 10
e <- sequence(4,1:4)
cat("e =", e)
## e = 1 2 3 4
matrix1 <- rbind(t(matrix1),e)
print(matrix1)
## [,1] [,2] [,3] [,4]
## 4 5 7 7
## 2 7 1 9
## 10 8 6 10
## 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:5,12,replace=TRUE),3,4)
print(A)
## [,1] [,2] [,3] [,4]
## [1,] 3 4 5 2
## [2,] 2 1 5 2
## [3,] 4 4 5 1
B <- matrix(sample(1:5,12,replace=TRUE),3,4)
print(B)
## [,1] [,2] [,3] [,4]
## [1,] 2 2 4 2
## [2,] 5 5 5 2
## [3,] 4 4 1 4
# dim(B) <- c(4,3)
C <- A%*%t(B)
print(C)
## [,1] [,2] [,3]
## [1,] 38 64 41
## [2,] 30 44 25
## [3,] 38 67 41
Create a matrix with 10 rows and 2 columns.
Extract a
sub-matrix from original matrix which includes the last 5 rows.
Q6 <- matrix(sample(1:10,20,replace=TRUE),10,2)
print(Q6)
## [,1] [,2]
## [1,] 4 8
## [2,] 7 1
## [3,] 1 9
## [4,] 1 9
## [5,] 4 5
## [6,] 4 4
## [7,] 2 5
## [8,] 4 3
## [9,] 4 4
## [10,] 10 2
Q6_sub <- tail(Q6,5)
# print(Q6[6:10,])
print(Q6_sub)
## [,1] [,2]
## [6,] 4 4
## [7,] 2 5
## [8,] 4 3
## [9,] 4 4
## [10,] 10 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.
matrix_A <- matrix(c(2,2,4,2),2,2,byrow=TRUE)
print(matrix_A)
## [,1] [,2]
## [1,] 2 2
## [2,] 4 2
matrix_B <- solve(matrix_A)
print(matrix_B)
## [,1] [,2]
## [1,] -0.5 0.5
## [2,] 1.0 -0.5
print(matrix_A%*%matrix_B)
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
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
# Q8a
col_num = ncol(mtcars)
cat("col_num =", col_num)
## col_num = 11
# Q8b
row_num = nrow(mtcars)
cat("row_num =", row_num)
## row_num = 32
# Q8c
print(mtcars[3,5])
## [1] 3.85
# Q8d
print(mtcars["Datsun 710","drat"])
## [1] 3.85
# Q8e
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
# Q8f
# print(mtcars$mpg)
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
# Q8g
# print(subset(mtcars, select=c("hp","qsec")))
print(mtcars[c("hp","qsec")])
## hp qsec
## Mazda RX4 110 16.46
## Mazda RX4 Wag 110 17.02
## Datsun 710 93 18.61
## Hornet 4 Drive 110 19.44
## Hornet Sportabout 175 17.02
## Valiant 105 20.22
## Duster 360 245 15.84
## Merc 240D 62 20.00
## Merc 230 95 22.90
## Merc 280 123 18.30
## Merc 280C 123 18.90
## Merc 450SE 180 17.40
## Merc 450SL 180 17.60
## Merc 450SLC 180 18.00
## Cadillac Fleetwood 205 17.98
## Lincoln Continental 215 17.82
## Chrysler Imperial 230 17.42
## Fiat 128 66 19.47
## Honda Civic 52 18.52
## Toyota Corolla 65 19.90
## Toyota Corona 97 20.01
## Dodge Challenger 150 16.87
## AMC Javelin 150 17.30
## Camaro Z28 245 15.41
## Pontiac Firebird 175 17.05
## Fiat X1-9 66 18.90
## Porsche 914-2 91 16.70
## Lotus Europa 113 16.90
## Ford Pantera L 264 14.50
## Ferrari Dino 175 15.50
## Maserati Bora 335 14.60
## Volvo 142E 109 18.60
Create a DataFrame using the following data.
SK020 is the
ProductCode, Enfagrow A+ is the ProductName and 36.79 is the Price.
Then, solve the following statements.
SK020,Enfagrow A+,36.79
SK042,Ayamas Nuget Crispy,9.99
SK013,100 Plus,6.49
SK066,Ali Cafe,8.99
SK079,Dettol
Natural,4.99
# Q9a
DataFrame <- data.frame(
ProductCode = c("SK020", "SK042", "SK013", "SK066", "SK079"),
ProductName = c("Enfagrow A+", "Ayamas Nuget Crispy", "100 Plus", "Ali Cafe", "Dettol Natural"),
Price = c(36.79, 9.99, 6.49, 8.99, 4.99)
)
print(DataFrame)
## ProductCode ProductName Price
## 1 SK020 Enfagrow A+ 36.79
## 2 SK042 Ayamas Nuget Crispy 9.99
## 3 SK013 100 Plus 6.49
## 4 SK066 Ali Cafe 8.99
## 5 SK079 Dettol Natural 4.99
# Q9b
Q9b <- DataFrame[order(DataFrame$ProductName),]
print(Q9b)
## ProductCode ProductName Price
## 3 SK013 100 Plus 6.49
## 4 SK066 Ali Cafe 8.99
## 2 SK042 Ayamas Nuget Crispy 9.99
## 5 SK079 Dettol Natural 4.99
## 1 SK020 Enfagrow A+ 36.79
# Q9c
new_product <- list("SK023", "Johnson PH", 5.99)
DataFrame <- rbind(DataFrame, new_product)
Q9c <- DataFrame[order(DataFrame$ProductName),]
print(Q9c)
## ProductCode ProductName Price
## 3 SK013 100 Plus 6.49
## 4 SK066 Ali Cafe 8.99
## 2 SK042 Ayamas Nuget Crispy 9.99
## 5 SK079 Dettol Natural 4.99
## 1 SK020 Enfagrow A+ 36.79
## 6 SK023 Johnson PH 5.99
# Q9d
Q9d <- DataFrame[which(DataFrame$Price>8),]
# Q9d <- DataFrame[DataFrame[,3]>8,]
print(Q9d)
## ProductCode ProductName Price
## 1 SK020 Enfagrow A+ 36.79
## 2 SK042 Ayamas Nuget Crispy 9.99
## 4 SK066 Ali Cafe 8.99
# Q9e
# Q9e <- DataFrame[order(DataFrame$Price),]
# print(Q9e)
# print(Q9e[1,])
# print(Q9e[nrow(Q9e),])
print(DataFrame[which.max(DataFrame$Price),])
## ProductCode ProductName Price
## 1 SK020 Enfagrow A+ 36.79
print(DataFrame[which.min(DataFrame$Price),])
## ProductCode ProductName Price
## 5 SK079 Dettol Natural 4.99
Q9e_subset <- subset(DataFrame, Price==max(Price) | Price==min(Price),
select=c(ProductName,ProductCode,Price))
print(Q9e_subset)
## ProductName ProductCode Price
## 1 Enfagrow A+ SK020 36.79
## 5 Dettol Natural SK079 4.99
# Q9f (unresolved)
Q9f <- subset(DataFrame, substr(ProductName,1,1) =="A", select=ProductName)
print(DataFrame[startsWith(DataFrame$ProductName,"A"),])
## ProductCode ProductName Price
## 2 SK042 Ayamas Nuget Crispy 9.99
## 4 SK066 Ali Cafe 8.99
print(Q9f)
## ProductName
## 2 Ayamas Nuget Crispy
## 4 Ali Cafe
print(nrow(Q9f))
## [1] 2