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,] 492.32076 79.35172 394.80962 237.7804 111.67995 48.74048
## [2,] 86.43967 413.40192 216.28094 285.4168 74.43347 222.22197
## [3,] 204.60742 49.99202 245.44610 44.9051 110.44032 250.69382
## [4,] 455.38063 238.48814 448.39213 261.1111 427.67241 323.39048
## [5,] 388.76780 403.49641 189.80986 347.5302 215.82916 275.34305
## [6,] 248.10135 312.83229 229.03251 284.9203 159.93001 370.03902
## [7,] 279.16558 17.08326 209.64988 121.8439 493.29907 150.58903
## [8,] 169.93724 239.71243 158.33669 246.2657 414.74335 191.68270
## [9,] 156.56273 68.21935 29.16724 164.9941 59.68993 242.66252
## [10,] 442.69180 173.92959 197.33417 126.4852 38.99019 373.13138
##
## $a
## [,1] [,2]
## [1,] 79.35172 111.67995
## [2,] 413.40192 74.43347
## [3,] 49.99202 110.44032
## [4,] 238.48814 427.67241
## [5,] 403.49641 215.82916
## [6,] 312.83229 159.93001
## [7,] 17.08326 493.29907
## [8,] 239.71243 414.74335
## [9,] 68.21935 59.68993
## [10,] 173.92959 38.99019
##
## $`b: cols 1-2 per-row sum`
## [,1]
## [1,] 571.6725
## [2,] 499.8416
## [3,] 254.5994
## [4,] 693.8688
## [5,] 792.2642
## [6,] 560.9336
## [7,] 296.2488
## [8,] 409.6497
## [9,] 224.7821
## [10,] 616.6214
##
## $`b: cols 1-2 total value`
## [1] 4920.482
##
## $c
## [1] 1 4
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