Read in the file femaleMiceWeights.csv and report the exact name of the column containing the weights.
file1<- read.csv("femaleMiceWeights.csv")
str(file1)
## 'data.frame': 24 obs. of 2 variables:
## $ Diet : Factor w/ 2 levels "chow","hf": 1 1 1 1 1 1 1 1 1 1 ...
## $ Bodyweight: num 21.5 28.1 24 23.4 23.7 ...
The [ and ] symbols can be used to extract specific rows and specific columns of the table. What is the entry in the 12th row and second column?
file1[12,2]
## [1] 26.25
You should have learned how to use the $ character to extract a column from a table and return it as a vector. Use $ to extract the weight column and report the weight of the mouse in the 11th row.
file1$Bodyweight[11]
## [1] 26.91
The length function returns the number of elements in a vector. How many mice are included in our dataset?
length(file1$Diet)
## [1] 24
To create a vector with the numbers 3 to 7, we can use seq(3,7) or, because they are consecutive, 3:7. View the data and determine what rows are associated with the high fat or hf diet. Then use the mean function to compute the average weight of these mice.
hf <- file1$Diet=="hf"
mean(file1$Bodyweight[hf==TRUE])
## [1] 26.83417
One of the functions we will be using often is sample. Read the help file for sample using ?sample. Now take a random sample of size 1 from the numbers 13 to 24 and report back the weight of the mouse represented by that row. Make sure to type set.seed(1) to ensure that everybody gets the same answer.
?sample
## starting httpd help server ... done
set.seed(1)
sample(13:24, 1)
## [1] 16
file1[16,2]
## [1] 25.34