Let x <- c(“Use R!”, “Data Science”, " “,”:)“,”y = f(x)“) Write the R code that will produce the single element vector”Use R!"
x <- c("Use R!", "Data Science", " ", ":)", "y = f(x)")
x[1]
## [1] "Use R!"
Add an element consisting of the number exp(2) and print out the new vector x
x[6] <- exp(2)
print(x)
## [1] "Use R!" "Data Science" " "
## [4] ":)" "y = f(x)" "7.38905609893065"
Write an expression that will get rid of the fifth element. (Without just retyping out the vector without that element.)
x <- c(2, 4, 6, 8, NA)
x <- x[-5]
print(x)
## [1] 2 4 6 8
Let x <- seq(3, 33, 3). Write an expression that will return the numbers of the third and ninth position.
x <- seq(3, 33, 3)
x[c(3,9)]
## [1] 9 27
Let a <- c(“A”, “B”, “C”), b <- c(1, 2, 3), c <- list(a, b). Find the vector c(1, 2, 3)
a <- c("A", "B", "C")
b <- c(1, 2, 3)
c <- list(a, b)
print(c)
## [[1]]
## [1] "A" "B" "C"
##
## [[2]]
## [1] 1 2 3
c[[2]]
## [1] 1 2 3
Let a <- c(“A”, “B”, “C”), b <- c(1, 2, 3), c <- list(a, b). Replace A with the letter B.
a <- c("A", "B", "C")
b <- c(1, 2, 3)
c <- list(a, b)
print(c)
## [[1]]
## [1] "A" "B" "C"
##
## [[2]]
## [1] 1 2 3
c[[1]][[1]] <- "B"
print(c)
## [[1]]
## [1] "B" "B" "C"
##
## [[2]]
## [1] 1 2 3
Let x <- list(a=1:10, b=“Hey!”, c= c(T, F, T, F)), write an R statement to add d = “KC” to the list x.
x <- list(a=1:10, b="Hey!", c = c(T, F, T, F))
x$d <- "KC"
print(x)
## $a
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $b
## [1] "Hey!"
##
## $c
## [1] TRUE FALSE TRUE FALSE
##
## $d
## [1] "KC"
For the admits dataframe, create a vector called student3 which contains Student 0003’s High School and ACT Score
admits <- data.frame(ADMIT_ID = c(paste0("000", 1:9),"0010"), ACT = round(runif(10, 17, 34)),
HIGH_SCHOOL_DESC = c("Blue Springs South", "Warrensburg High School", "Odessa R-VII SR High School", "Blue Springs South", "Lees Summit North High School", "Lees Summit West High School",
rep("Warrensburg High School", 3), "Platt County R-III High School"), stringsAsFactors = FALSE)
student3 <- c(admits$HIGH_SCHOOL_DESC[3], admits$ACT[3])
print(student3)
## [1] "Odessa R-VII SR High School" "24"
student3 <- c(admits[3,3], admits[3,2])
student3
## [1] "Odessa R-VII SR High School" "24"