Please email me a copy of your R script for this assignment by start of lab on Friday January 26th with your first and last name followed by assignment 1 as the file name (e.g. ‘marissa_dyck_assignment1.R’)
You should always be following best coding practices (see Intro to R module 1) but especially for assingment submissions. Please make sure each problem has its own header so that I can easily navigate to your answers and that your code is well organized with spaces as described in the best coding practices section and comments as needed.
Create a vector called ‘myvec’ using any of the methods you learned with numbers 1 to 10. Note there are multiple ways to do this.
# (1 pt)
# answer 1 (the most parsimonious)
myvec <- 1:10
# answer 2
myvec <- c(1, 10)
# answer 3 (time consuming)
myvec <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# answer 4 (unecessarily complicated for this exercise but works)
myvec <- seq(1, 10, by = 1)
myvec
## [1] 1 2 3 4 5 6 7 8 9 10
rbind()
Create a 3 row by 2 column matrix named ‘mymat’. Use the
rbind()
function to bind the following
three rows/vectors together:
c(1,4)
c(2,5)
c(3,6)
# (1 pt)
mymat <- rbind(
c(1,4),
c(2,5),
c(3,6)
)
mymat
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
Get the names of columns in the data frame you created earlier
‘mydf’. Hint see the R functions to explore data section. Then
extract all rows for column 5 by name, do the same thing using the
element position e.g. []
# code to create mydf
my.data <- data.frame(
Obs.Id = 1:100,
Treatment = rep(c("A","B","C","D","E"),
each = 20),
Block = rep(1:20,
times = 5),
Germination = rpois(100,
lambda = rep(c(1,5,4,7,1),
each = 20)), # random poisson variable
AvgHeight = rnorm(100,
mean = rep(c(10,30,31,25,35,7),
each = 20))
)
mydf = my.data[21:30, ]
# answer for problem 1
# (3 pts)
names(mydf)
## [1] "Obs.Id" "Treatment" "Block" "Germination" "AvgHeight"
mydf$AvgHeight
## [1] 30.97511 30.33584 30.28112 29.00601 28.67642 29.89063 30.41488 30.25876
## [9] 30.08120 28.46604
mydf[ , 5]
## [1] 30.97511 30.33584 30.28112 29.00601 28.67642 29.89063 30.41488 30.25876
## [9] 30.08120 28.46604
Create a new matrix called ‘mymat2’ that includes all the data from
columns 3 to 5 of data frame mydf. HINT: use the
as.matrix()
function to coerce a data frame into a matrix.
Since we didn’t cover this function you may need to look it up in the
help files.
Note your values for some columns may be slighly different since the code to create mydf uses random number generators.
# (1 pt)
mymat2 <- as.matrix(mydf[ , 3:5])
mymat2
## Block Germination AvgHeight
## 21 1 3 30.97511
## 22 2 5 30.33584
## 23 3 7 30.28112
## 24 4 6 29.00601
## 25 5 3 28.67642
## 26 6 5 29.89063
## 27 7 2 30.41488
## 28 8 2 30.25876
## 29 9 6 30.08120
## 30 10 5 28.46604
Create a list named ‘mylist’ that is composed of a
- vector: 1:3,
- a matrix: matrix(1:6, nrow = 3, ncol = 2),
- and a data frame: data.frame(x =c (1, 2, 3), y = c(TRUE, FALSE, TRUE),
z = c(“a”, “a”, “b”)).
#create an empty list
mylist <- list()
# add a vector of 1 to 3 to the list
mylist[[1]] <- 1:3
# add a matrix to the list
mylist[[2]] <- matrix(1:6,
nrow = 3,
ncol = 2)
# add a data frame to the list
mylist[[3]] <- data.frame(x = c(1, 2,3 ),
y = c(TRUE, FALSE, TRUE),
z = c("a", "a", "b"))
# print the list
mylist
## [[1]]
## [1] 1 2 3
##
## [[2]]
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
##
## [[3]]
## x y z
## 1 1 TRUE a
## 2 2 FALSE a
## 3 3 TRUE b
Extract the second and third observation from the 1st column of the data frame in ‘mylist’ (the list created above).
# multiple ways to do this
# answer 1 - reference the element in the list [[3]] then the rows and columns you want from that element [2:3, 1]
mylist[[3]][2:3, 1]
## [1] 2 3
# answer 2 call mylist then reference the position of the data frame in the list [[3]], then the column in the data frame [[1]], and finally the observations within that columns c(2, 3)
mylist[[3]][[1]][c(2, 3)]
## [1] 2 3