Create a vector called scores containing the values: 72, 85, 90, 66,
88, 91, 70.
a. what is the length of scores?
b.
Extract the 2nd, 4th, and 7th elements.
c. Find all values
in scores that are greater than 80.
scores <- c(72, 85, 90, 66, 88, 91, 70)
a <- length(scores)
b <- scores[c(2, 4, 7)]
c <- scores[scores > 80]
one <- list("data" = scores, "a" = a, "b" = b, "c" = c)
one
## $data
## [1] 72 85 90 66 88 91 70
##
## $a
## [1] 7
##
## $b
## [1] 85 66 70
##
## $c
## [1] 85 90 88 91
Use runif() function to generate 60 random numbers between 10 and 500
and create a 10 by 6 matrix using these numbers (fill by rows).
a. Extract columns 2 and 5 as a new matrix.
b. Add up
columns 1 and 2.
c. Identify all rows where the value in
column 3 is greater than 250.
x <- runif(60, 10, 500)
x <- matrix(x, nrow = 10, ncol = 6, byrow = TRUE)
column_1 <- x[, 1]
column_2 <- x[, 2]
column_3 <- x[, 3]
column_5 <- x[, 5]
a <- matrix(c(column_2, column_5), 10, 2)
b_1 <- c(column_1 + column_2)
b_1 <- matrix(b_1, 10, 1)
b_2 <- sum(column_1 + column_2)
c <- which(column_3 > 250)
two <- list("data" = x, "a" = a, "b: cols 1-2 per-row sum" = b_1, "b: cols 1-2 total value" = b_2, "c" = c)
two
## $data
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 296.75603 468.4609 214.20394 460.29174 297.6883 131.48335
## [2,] 38.25636 144.1139 230.83900 388.22040 409.3830 82.25202
## [3,] 406.45154 195.3530 475.31271 150.61448 485.3708 439.75351
## [4,] 181.01861 143.9536 312.06087 23.26994 323.3875 170.17119
## [5,] 281.24270 498.4868 258.63220 25.04357 305.4974 195.55690
## [6,] 348.65033 292.6991 138.78289 442.66992 372.3343 173.13807
## [7,] 318.21321 304.2256 84.55716 121.48166 281.4580 35.45112
## [8,] 445.52200 464.1509 460.15015 253.02151 74.2787 35.88497
## [9,] 104.22535 257.1227 146.44467 291.06214 104.0273 316.15085
## [10,] 467.73128 109.5587 146.63187 139.93950 202.4149 335.28071
##
## $a
## [,1] [,2]
## [1,] 468.4609 297.6883
## [2,] 144.1139 409.3830
## [3,] 195.3530 485.3708
## [4,] 143.9536 323.3875
## [5,] 498.4868 305.4974
## [6,] 292.6991 372.3343
## [7,] 304.2256 281.4580
## [8,] 464.1509 74.2787
## [9,] 257.1227 104.0273
## [10,] 109.5587 202.4149
##
## $`b: cols 1-2 per-row sum`
## [,1]
## [1,] 765.2169
## [2,] 182.3702
## [3,] 601.8045
## [4,] 324.9722
## [5,] 779.7295
## [6,] 641.3494
## [7,] 622.4388
## [8,] 909.6729
## [9,] 361.3481
## [10,] 577.2900
##
## $`b: cols 1-2 total value`
## [1] 5766.193
##
## $c
## [1] 3 4 5 8
Create the following two data frames: (example)
a. Combine
the two data frames column-wise.
b. What class of data is
in each column of the combined data frame?
c. Replace row
names with letters from A to G.
age <- c(25, 31, 23, 52, 76, 49, 26)
height <- c(177, 163, 190, 179, 163, 183, 164)
weight <- c(57, 69, 83, 75, 70, 83, 53)
working <- c("Yes", "No", "No", "Yes", "Yes", "No", "Yes")
row_cat_1 <- c("Alex", "Lilly", "Mark", "Oliver", "Martha", "Lucas", "Caroline")
row_cat_2 <- c("A", "B", "C", "D", "E", "F", "G")
data_1 <- data.frame(age, height, weight)
data_2 <- data.frame(working)
rownames(data_1) <- row_cat_1
rownames(data_2) <- row_cat_1
a <- cbind(data_1, data_2)
b <- sapply(a, class)
c <- a
rownames(c) <- row_cat_2
three <- list("data_1" = data_1, "data_2" = data_2, "a" = a, "b" = b, "c" = c)
three
## $data_1
## age height weight
## Alex 25 177 57
## Lilly 31 163 69
## Mark 23 190 83
## Oliver 52 179 75
## Martha 76 163 70
## Lucas 49 183 83
## Caroline 26 164 53
##
## $data_2
## working
## Alex Yes
## Lilly No
## Mark No
## Oliver Yes
## Martha Yes
## Lucas No
## Caroline Yes
##
## $a
## age height weight working
## Alex 25 177 57 Yes
## Lilly 31 163 69 No
## Mark 23 190 83 No
## Oliver 52 179 75 Yes
## Martha 76 163 70 Yes
## Lucas 49 183 83 No
## Caroline 26 164 53 Yes
##
## $b
## age height weight working
## "numeric" "numeric" "numeric" "character"
##
## $c
## age height weight working
## A 25 177 57 Yes
## B 31 163 69 No
## C 23 190 83 No
## D 52 179 75 Yes
## E 76 163 70 Yes
## F 49 183 83 No
## G 26 164 53 Yes