Vectors
x <- c(1, 2, 3, 4, 5)
x <- 1:5
x <- seq(1,5, by = 1)
set.seed(1)
x2 <- sample(c(TRUE, FALSE), p = c(1/3, 2/3), size = 8, replace = T)
x2
## [1] FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE
# give x2 names c("X1", ... "X8")
names(x2) <- c("X1", "X2",
"X3", "X4",
"X5", "X6",
"X7", "X8") #list or vector
names(x2) <- paste("X", 1:8, sep = "")
x3 <- c("I", "took", "this", "class", "back", "in", "my", "day")
x4 <- c("I", 1, TRUE) # will all be strings since we only can have 1 type
Matrices
x_mat <- matrix(x, nrow = 1)
dim(x_mat)
## [1] 1 5
x_mat2 <- matrix(c(1,1,2,3,5,8,
2,4,8,16,32,64,
0,1,0,-1,0,1),
nrow = 3, byrow = TRUE)
x_mat2
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 2 3 5 8
## [2,] 2 4 8 16 32 64
## [3,] 0 1 0 -1 0 1
x2_mat <- matrix(x2, nrow = 4)
x3_mat <- matrix(x3, nrow = 2)
List
my_list_1
)my_list_1 <- list(x2, x_mat2, x2)
my_list_1[1]
## [[1]]
## X1 X2 X3 X4 X5 X6 X7 X8
## FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE
typeof(my_list_1[1])
## [1] "list"
my_list_1[[1]]
## X1 X2 X3 X4 X5 X6 X7 X8
## FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE
typeof(my_list_1[[1]])
## [1] "logical"
is.vector(my_list_1[[1]])
## [1] TRUE
my_list_1[1:2]
## [[1]]
## X1 X2 X3 X4 X5 X6 X7 X8
## FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE
##
## [[2]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 2 3 5 8
## [2,] 2 4 8 16 32 64
## [3,] 0 1 0 -1 0 1
my_list_1[c("x2", "x_mat2")]
## [[1]]
## NULL
##
## [[2]]
## NULL
my_list_1
)names(my_list_1) <- c("x2","x_mat2")
#my_list_1 <-
my_list_2
)data.frame 1. make a data frame with all vector components… (my_df
)
my_df <- data.frame(x[1:3],x2[1:3],x3[1:3],x4[1:3])
colnames(my_df) <- paste("banana", 1:4)
names(my_df) <- paste("apple", 1:4)
rownames(my_df) <- paste("pear", 1:3)
my_df
## apple 1 apple 2 apple 3 apple 4
## pear 1 1 FALSE I I
## pear 2 2 FALSE took 1
## pear 3 3 FALSE this TRUE
x_mat2
(my_df_2
). What will the columns names be?my_df_2 <- data.frame(x_mat2)
x
are greater than or equal to 5?which(x >= 5)
## [1] 5
x_mat2
are greater than 5? Comment on format?which(x_mat2 >= 5)
## [1] 8 11 13 14 16 17
x_mat2[x_mat2 >= 5]
## [1] 8 16 5 32 8 64
x_mat2
that are greater than 5 to be negative 1.x_mat2[x_mat2 >= 5] <- -1
x2
(what are all the ways we could do this?)16
out of x_mat2
(what are all the ways we could do this?)my_df
x_mat2
equal to 0print out each element of x2
individually .5. storage in count_true
out the number of element up to i
that have been true in the x2
vector
create a print out of the binomial expansion
x1
1 if the index is odd and -1 if the index is evenx2
entries so that if they’re false - they’re now true if the index is even (else leave as is)