Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
Problem 1
x <-c(5,7,3,10,1)
x[3]
## [1] 3
x[-2]
## [1] 5 3 10 1
x[c(2,3,5)]
## [1] 7 3 1
x[4] <- 17
x[c(1,3)] <- 4
Problem 2
x <- c(1,2,3,7,6,8,3,12,0,17)
length(x)
## [1] 10
sort(x,decreasing = T)
## [1] 17 12 8 7 6 3 3 2 1 0
mean(x)
## [1] 5.9
Problem 3
cat("PSTAT 10 HW 1 Question 3")
## PSTAT 10 HW 1 Question 3
Problem 4
fun <- seq(2.25, 3.0, by = 0.25)
Problem 5
re <- c(rep(1,3),rep(2,3),rep(3,3),4)
Problem 6
re <- c(rep(1:3,each=3),4)
Problem 7
x <- c(1,2,3,4,5,6)
y <- c(10,20,30,40,50,60)
x+y
## [1] 11 22 33 44 55 66
The operator + in R performs element-wise addition when used on two vectors of the same length. This means each element in vector x is added to the corresponding element in vector y:
1 + 10 = 11,2 + 20 = 22,3 + 30 = 33,4 + 40 = 44,5 + 50 = 55,6 + 60 = 66
Problem 8
z <-c(1.0,2.0,4.6,5.5)
class(z)
## [1] "numeric"
as.integer(z)
## [1] 1 2 4 5
as.logical(z)
## [1] TRUE TRUE TRUE TRUE
as.character(z)
## [1] "1" "2" "4.6" "5.5"
Problem 9
A <-c("PSTAT 10")
B <-c("HOMEWORK 1, Q9")
paste(A,B)
## [1] "PSTAT 10 HOMEWORK 1, Q9"
A == B
## [1] FALSE
Problem 10
mileage <- c(65311, 65624, 65908)
mile_differences <- diff(mileage)
half_steps <- seq(1, 10, by = 0.5)
m <- matrix(1:20, nrow = 5, ncol = 4)
subset_m <- m[1:3, 2:4]