5 Functions
In the last section, you were asked to convert the “smoker” column to logical values. The solution is fairly simple:
$smoker == "yes" birthweight
## [1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE
## [13] TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE
## [25] FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
## [37] TRUE FALSE FALSE TRUE TRUE FALSE
$smoker <- (birthweight$smoker == "yes") birthweight
birthweight
ID | birth.date | location | length | birthweight | head.circumference | weeks.gestation | smoker | maternal.age | maternal.cigarettes | maternal.height | maternal.prepregnant.weight | paternal.age | paternal.education | paternal.cigarettes | paternal.height | low.birthweight | geriatric.pregnancy |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1107 | 1/25/1967 | General | 52 | 3.23 | 36 | 38 | FALSE | 31 | 0 | 164 | 57 | NA | NA | NA | NA | FALSE | FALSE |
697 | 2/6/1967 | Silver Hill | 48 | 3.03 | 35 | 39 | FALSE | 27 | 0 | 162 | 62 | 27 | 14 | 0 | 178 | FALSE | FALSE |
1683 | 2/14/1967 | Silver Hill | 53 | 3.35 | 33 | 41 | FALSE | 27 | 0 | 164 | 62 | 37 | 14 | 0 | 170 | FALSE | FALSE |
27 | 3/9/1967 | Silver Hill | 53 | 3.55 | 37 | 41 | TRUE | 37 | 25 | 161 | 66 | 46 | NA | 0 | 175 | FALSE | TRUE |
1522 | 3/13/1967 | Memorial | 50 | 2.74 | 33 | 39 | TRUE | 21 | 17 | 156 | 53 | 24 | 12 | 7 | 179 | FALSE | FALSE |
569 | 3/23/1967 | Memorial | 50 | 2.51 | 35 | 39 | TRUE | 22 | 7 | 159 | 52 | 23 | 14 | 25 | NA | TRUE | FALSE |
365 | 4/23/1967 | Memorial | 52 | 3.53 | 37 | 40 | TRUE | 26 | 25 | 170 | 62 | 30 | 10 | 25 | 181 | FALSE | FALSE |
808 | 5/5/1967 | Silver Hill | 48 | 2.92 | 33 | 34 | FALSE | 26 | 0 | 167 | 64 | 25 | 12 | 25 | 175 | FALSE | FALSE |
1369 | 6/4/1967 | Silver Hill | 49 | 3.18 | 34 | 38 | TRUE | 31 | 25 | 162 | 57 | 32 | 16 | 50 | 194 | FALSE | FALSE |
1023 | 6/7/1967 | Memorial | 52 | 3.00 | 35 | 38 | TRUE | 30 | 12 | 165 | 64 | 38 | 14 | 50 | 180 | FALSE | FALSE |
822 | 6/14/1967 | Memorial | 50 | 3.42 | 35 | 38 | FALSE | 20 | 0 | 157 | 48 | 22 | 14 | 0 | 179 | FALSE | FALSE |
1272 | 6/20/1967 | Memorial | 53 | 2.75 | 32 | 40 | TRUE | 37 | 50 | 168 | 61 | 31 | 16 | 0 | 173 | FALSE | TRUE |
1262 | 6/25/1967 | Silver Hill | 53 | 3.19 | 34 | 41 | TRUE | 27 | 35 | 163 | 51 | 31 | 16 | 25 | 185 | FALSE | FALSE |
575 | 7/12/1967 | Memorial | 50 | 2.78 | 30 | 37 | TRUE | 19 | 7 | 165 | 60 | 20 | 14 | 0 | 183 | FALSE | FALSE |
1016 | 7/13/1967 | Silver Hill | 53 | 4.32 | 36 | 40 | FALSE | 19 | 0 | 171 | 62 | 19 | 12 | 0 | 183 | FALSE | FALSE |
792 | 9/7/1967 | Memorial | 53 | 3.64 | 38 | 40 | TRUE | 20 | 2 | 170 | 59 | 24 | 12 | 12 | 185 | FALSE | FALSE |
820 | 10/7/1967 | General | 52 | 3.77 | 34 | 40 | FALSE | 24 | 0 | 157 | 50 | 31 | 16 | 0 | 173 | FALSE | FALSE |
752 | 10/19/1967 | General | 49 | 3.32 | 36 | 40 | TRUE | 27 | 12 | 152 | 48 | 37 | 12 | 25 | 170 | FALSE | FALSE |
619 | 11/1/1967 | Memorial | 52 | 3.41 | 33 | 39 | TRUE | 23 | 25 | 181 | 69 | 23 | 16 | 2 | 181 | FALSE | FALSE |
1764 | 12/7/1967 | Silver Hill | 58 | 4.57 | 39 | 41 | TRUE | 32 | 12 | 173 | 70 | 38 | 14 | 25 | 180 | FALSE | FALSE |
1081 | 12/14/1967 | Silver Hill | 54 | 3.63 | 38 | 38 | FALSE | 18 | 0 | 172 | 50 | 20 | 12 | 7 | 172 | FALSE | FALSE |
516 | 1/8/1968 | Silver Hill | 47 | 2.66 | 33 | 35 | TRUE | 20 | 35 | 170 | 57 | 23 | 12 | 50 | 186 | TRUE | FALSE |
272 | 1/10/1968 | Memorial | 52 | 3.86 | 36 | 39 | TRUE | 30 | 25 | 170 | 78 | 40 | 16 | 50 | 178 | FALSE | FALSE |
321 | 1/21/1968 | Silver Hill | 48 | 3.11 | 33 | 37 | FALSE | 28 | 0 | 158 | 54 | 39 | 10 | 0 | 171 | FALSE | FALSE |
1636 | 2/2/1968 | Silver Hill | 51 | 3.93 | 38 | 38 | FALSE | 29 | 0 | 165 | 61 | NA | NA | NA | NA | FALSE | FALSE |
1360 | 2/16/1968 | General | 56 | 4.55 | 34 | 44 | FALSE | 20 | 0 | 162 | 57 | 23 | 10 | 35 | 179 | FALSE | FALSE |
1388 | 2/22/1968 | Memorial | 51 | 3.14 | 33 | 41 | TRUE | 22 | 7 | 160 | 53 | 24 | 16 | 12 | 176 | FALSE | FALSE |
1363 | 4/2/1968 | General | 48 | 2.37 | 30 | 37 | TRUE | 20 | 7 | 163 | 47 | 20 | 10 | 35 | 185 | TRUE | FALSE |
1058 | 4/24/1968 | Silver Hill | 53 | 3.15 | 34 | 40 | FALSE | 29 | 0 | 167 | 60 | 30 | 16 | NA | 182 | FALSE | FALSE |
755 | 4/25/1968 | Memorial | 53 | 3.20 | 33 | 41 | FALSE | 21 | 0 | 155 | 55 | 25 | 14 | 25 | 183 | FALSE | FALSE |
462 | 6/19/1968 | Silver Hill | 58 | 4.10 | 39 | 41 | FALSE | 35 | 0 | 172 | 58 | 31 | 16 | 25 | 185 | FALSE | TRUE |
300 | 7/18/1968 | Silver Hill | 46 | 2.05 | 32 | 35 | TRUE | 41 | 7 | 166 | 57 | 37 | 14 | 25 | 173 | TRUE | TRUE |
1088 | 7/24/1968 | General | 51 | 3.27 | 36 | 40 | FALSE | 24 | 0 | 168 | 53 | 29 | 16 | 0 | 181 | FALSE | FALSE |
57 | 8/12/1968 | Memorial | 51 | 3.32 | 38 | 39 | TRUE | 23 | 17 | 157 | 48 | NA | NA | NA | NA | FALSE | FALSE |
553 | 8/17/1968 | Silver Hill | 54 | 3.94 | 37 | 42 | FALSE | 24 | 0 | 175 | 66 | 30 | 12 | 0 | 184 | FALSE | FALSE |
1191 | 9/7/1968 | General | 53 | 3.65 | 33 | 42 | FALSE | 21 | 0 | 165 | 61 | 21 | 10 | 25 | 185 | FALSE | FALSE |
431 | 9/16/1968 | Silver Hill | 48 | 1.92 | 30 | 33 | TRUE | 20 | 7 | 161 | 50 | 20 | 10 | 35 | 180 | TRUE | FALSE |
1313 | 9/27/1968 | Silver Hill | 43 | 2.65 | 32 | 33 | FALSE | 24 | 0 | 149 | 45 | 26 | 16 | 0 | 169 | TRUE | FALSE |
1600 | 10/9/1968 | General | 53 | 2.90 | 34 | 39 | FALSE | 19 | 0 | 165 | 57 | NA | NA | NA | NA | FALSE | FALSE |
532 | 10/25/1968 | General | 53 | 3.59 | 34 | 40 | TRUE | 31 | 12 | 163 | 49 | 41 | 12 | 50 | 191 | FALSE | FALSE |
223 | 12/11/1968 | General | 50 | 3.87 | 33 | 45 | TRUE | 28 | 25 | 163 | 54 | 30 | 16 | 0 | 183 | FALSE | FALSE |
1187 | 12/19/1968 | Silver Hill | 53 | 4.07 | 38 | 44 | FALSE | 20 | 0 | 174 | 68 | 26 | 14 | 25 | 189 | FALSE | FALSE |
Converting the dates (currently stored as characters) to a more usable format is not as simple, and will require a function.
R is filled with functions. We have already used a few: read.csv()
, class()
, dim()
, mean()
, colnames()
, as.logical()
, and as.numeric()
. To find the documentation on a function, use the ?
character. This opens a page containing details on the function in the help pane.
5.1 Basic functions
?tabletable(birthweight$geriatric.pregnancy, birthweight$low.birthweight)
##
## FALSE TRUE
## FALSE 33 5
## TRUE 3 1
Functions are invoked by name, with any arguments provided inside of parentheses. For example, the chisq.test()
function, which runs a Chi-squared test, requires either a matrix or two vectors as arguments, and provides a number of options that may be set by the user.
?chisq.testchisq.test(birthweight$geriatric.pregnancy, birthweight$low.birthweight)
## Warning in chisq.test(birthweight$geriatric.pregnancy,
## birthweight$low.birthweight): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: birthweight$geriatric.pregnancy and birthweight$low.birthweight
## X-squared = 2.7398e-31, df = 1, p-value = 1
If no parameters are provided to the options, the default values are used. In some cases, this works well.
mean(birthweight$birthweight[birthweight$geriatric.pregnancy])
## [1] 3.1125
# the ! character is used for negation
mean(birthweight$birthweight[!birthweight$geriatric.pregnancy])
## [1] 3.333947
Other times, it is necessary to adjust the options in order for a function to behave as you expect.
mean(birthweight$paternal.age)
## [1] NA
mean(birthweight$paternal.age, na.rm = TRUE)
## [1] 28.76316
sd(birthweight$paternal.age, na.rm = TRUE)
## [1] 7.061254
5.2 Combining functions
The “birth.date” column is currently stored as a character vector. But what if we wanted to look at the birth weights of babies born in December? Let’s split the day, month, and year into separate columns.
R has a function called strsplit()
:
?strsplitstrsplit(birthweight$birth.date, split = "/")
## [[1]]
## [1] "1" "25" "1967"
##
## [[2]]
## [1] "2" "6" "1967"
##
## [[3]]
## [1] "2" "14" "1967"
##
## [[4]]
## [1] "3" "9" "1967"
##
## [[5]]
## [1] "3" "13" "1967"
##
## [[6]]
## [1] "3" "23" "1967"
##
## [[7]]
## [1] "4" "23" "1967"
##
## [[8]]
## [1] "5" "5" "1967"
##
## [[9]]
## [1] "6" "4" "1967"
##
## [[10]]
## [1] "6" "7" "1967"
##
## [[11]]
## [1] "6" "14" "1967"
##
## [[12]]
## [1] "6" "20" "1967"
##
## [[13]]
## [1] "6" "25" "1967"
##
## [[14]]
## [1] "7" "12" "1967"
##
## [[15]]
## [1] "7" "13" "1967"
##
## [[16]]
## [1] "9" "7" "1967"
##
## [[17]]
## [1] "10" "7" "1967"
##
## [[18]]
## [1] "10" "19" "1967"
##
## [[19]]
## [1] "11" "1" "1967"
##
## [[20]]
## [1] "12" "7" "1967"
##
## [[21]]
## [1] "12" "14" "1967"
##
## [[22]]
## [1] "1" "8" "1968"
##
## [[23]]
## [1] "1" "10" "1968"
##
## [[24]]
## [1] "1" "21" "1968"
##
## [[25]]
## [1] "2" "2" "1968"
##
## [[26]]
## [1] "2" "16" "1968"
##
## [[27]]
## [1] "2" "22" "1968"
##
## [[28]]
## [1] "4" "2" "1968"
##
## [[29]]
## [1] "4" "24" "1968"
##
## [[30]]
## [1] "4" "25" "1968"
##
## [[31]]
## [1] "6" "19" "1968"
##
## [[32]]
## [1] "7" "18" "1968"
##
## [[33]]
## [1] "7" "24" "1968"
##
## [[34]]
## [1] "8" "12" "1968"
##
## [[35]]
## [1] "8" "17" "1968"
##
## [[36]]
## [1] "9" "7" "1968"
##
## [[37]]
## [1] "9" "16" "1968"
##
## [[38]]
## [1] "9" "27" "1968"
##
## [[39]]
## [1] "10" "9" "1968"
##
## [[40]]
## [1] "10" "25" "1968"
##
## [[41]]
## [1] "12" "11" "1968"
##
## [[42]]
## [1] "12" "19" "1968"
This is a good start. However, the output of strsplit()
is a list containing 42 vectors of length 3, while the columns of birthweight are vectors of length 42.
A note on lists:
Lists are ordered collections of objects, which can be of any type. Unlike vectors, lists may contain elements of different types. To subset a list, use the $
or [[
operator.
The unlist()
function will “flatten” a list into a vector.
unlist(strsplit(birthweight$birth.date, split = "/"))
## [1] "1" "25" "1967" "2" "6" "1967" "2" "14" "1967" "3"
## [11] "9" "1967" "3" "13" "1967" "3" "23" "1967" "4" "23"
## [21] "1967" "5" "5" "1967" "6" "4" "1967" "6" "7" "1967"
## [31] "6" "14" "1967" "6" "20" "1967" "6" "25" "1967" "7"
## [41] "12" "1967" "7" "13" "1967" "9" "7" "1967" "10" "7"
## [51] "1967" "10" "19" "1967" "11" "1" "1967" "12" "7" "1967"
## [61] "12" "14" "1967" "1" "8" "1968" "1" "10" "1968" "1"
## [71] "21" "1968" "2" "2" "1968" "2" "16" "1968" "2" "22"
## [81] "1968" "4" "2" "1968" "4" "24" "1968" "4" "25" "1968"
## [91] "6" "19" "1968" "7" "18" "1968" "7" "24" "1968" "8"
## [101] "12" "1968" "8" "17" "1968" "9" "7" "1968" "9" "16"
## [111] "1968" "9" "27" "1968" "10" "9" "1968" "10" "25" "1968"
## [121] "12" "11" "1968" "12" "19" "1968"
When functions are “nested,” the operation in the innermost set of parentheses is performed first. In the example above, the unlist()
function is applied to the output of strsplit()
, changing a list to a vector. Since the goal was to split one column into three columns, this combination of functions is not effective.
We are going to need to apply some function to each item in the list that allows us to retrieve the first element of the vector.
5.3 The apply() family of functions
One of the most useful groups of functions in R is the apply()
family of functions. This group of functions execute a provided function on every element of a data structure.
apply takes a matrix, applies a function either by row or by column, and returns a vector.
# by row
apply(birthweight[,c("maternal.cigarettes", "paternal.cigarettes")], 1, sum)
## [1] NA 0 0 25 24 32 50 25 75 62 0 50 60 7 0 14 0 37 27 37 7 85 75 0 NA
## [26] 35 19 42 NA 25 25 32 0 NA 0 25 42 0 NA 62 25 25
# by column
apply(birthweight[,c("maternal.cigarettes", "paternal.cigarettes")], 2, sum)
## maternal.cigarettes paternal.cigarettes
## 396 NA
mapply takes a function and applies it to the elements of one or more vectors.
mapply(sum, birthweight$maternal.cigarettes, birthweight$paternal.cigarettes)
## [1] NA 0 0 25 24 32 50 25 75 62 0 50 60 7 0 14 0 37 27 37 7 85 75 0 NA
## [26] 35 19 42 NA 25 25 32 0 NA 0 25 42 0 NA 62 25 25
tapply takes two vectors, applies a function to the subsets of the first based on the categories in the second vector, and returns a table.
tapply(birthweight$birthweight, birthweight$smoker, mean)
## FALSE TRUE
## 3.509500 3.134091
lapply takes a list, applies a function to each element, and returns a list.
sapply takes a list, applies a function to each element, and returns a list that has been simplified as much as possible.
lapply(strsplit(birthweight$birth.date, split = "/"), '[[', 1)
## [[1]]
## [1] "1"
##
## [[2]]
## [1] "2"
##
## [[3]]
## [1] "2"
##
## [[4]]
## [1] "3"
##
## [[5]]
## [1] "3"
##
## [[6]]
## [1] "3"
##
## [[7]]
## [1] "4"
##
## [[8]]
## [1] "5"
##
## [[9]]
## [1] "6"
##
## [[10]]
## [1] "6"
##
## [[11]]
## [1] "6"
##
## [[12]]
## [1] "6"
##
## [[13]]
## [1] "6"
##
## [[14]]
## [1] "7"
##
## [[15]]
## [1] "7"
##
## [[16]]
## [1] "9"
##
## [[17]]
## [1] "10"
##
## [[18]]
## [1] "10"
##
## [[19]]
## [1] "11"
##
## [[20]]
## [1] "12"
##
## [[21]]
## [1] "12"
##
## [[22]]
## [1] "1"
##
## [[23]]
## [1] "1"
##
## [[24]]
## [1] "1"
##
## [[25]]
## [1] "2"
##
## [[26]]
## [1] "2"
##
## [[27]]
## [1] "2"
##
## [[28]]
## [1] "4"
##
## [[29]]
## [1] "4"
##
## [[30]]
## [1] "4"
##
## [[31]]
## [1] "6"
##
## [[32]]
## [1] "7"
##
## [[33]]
## [1] "7"
##
## [[34]]
## [1] "8"
##
## [[35]]
## [1] "8"
##
## [[36]]
## [1] "9"
##
## [[37]]
## [1] "9"
##
## [[38]]
## [1] "9"
##
## [[39]]
## [1] "10"
##
## [[40]]
## [1] "10"
##
## [[41]]
## [1] "12"
##
## [[42]]
## [1] "12"
sapply(strsplit(birthweight$birth.date, split = "/"), '[[', 1)
## [1] "1" "2" "2" "3" "3" "3" "4" "5" "6" "6" "6" "6" "6" "7" "7"
## [16] "9" "10" "10" "11" "12" "12" "1" "1" "1" "2" "2" "2" "4" "4" "4"
## [31] "6" "7" "7" "8" "8" "9" "9" "9" "10" "10" "12" "12"
The result of this nested pair of functions is a vector of months encoded as numbers stored as characters.
5.4 Custom functions
What if we want to do this for all three fields, but we don’t want to write the code three times?
Take a few minutes to dissect the following function, working from the inside out. What does each piece do?
# custom function takes a vector of dates and returns a data frame with columns day, month, and year
<- function(date_vector){
split_MMDDYYYY = lapply(seq(1:3), function(i){
date_list as.integer(sapply(strsplit(date_vector, split = "/"), '[[', i))
})names(date_list) = c("month", "day", "year")
as.data.frame(do.call("cbind", date_list))
}
split_MMDDYYYY(birthweight$birth.date)
month | day | year |
---|---|---|
1 | 25 | 1967 |
2 | 6 | 1967 |
2 | 14 | 1967 |
3 | 9 | 1967 |
3 | 13 | 1967 |
3 | 23 | 1967 |
4 | 23 | 1967 |
5 | 5 | 1967 |
6 | 4 | 1967 |
6 | 7 | 1967 |
6 | 14 | 1967 |
6 | 20 | 1967 |
6 | 25 | 1967 |
7 | 12 | 1967 |
7 | 13 | 1967 |
9 | 7 | 1967 |
10 | 7 | 1967 |
10 | 19 | 1967 |
11 | 1 | 1967 |
12 | 7 | 1967 |
12 | 14 | 1967 |
1 | 8 | 1968 |
1 | 10 | 1968 |
1 | 21 | 1968 |
2 | 2 | 1968 |
2 | 16 | 1968 |
2 | 22 | 1968 |
4 | 2 | 1968 |
4 | 24 | 1968 |
4 | 25 | 1968 |
6 | 19 | 1968 |
7 | 18 | 1968 |
7 | 24 | 1968 |
8 | 12 | 1968 |
8 | 17 | 1968 |
9 | 7 | 1968 |
9 | 16 | 1968 |
9 | 27 | 1968 |
10 | 9 | 1968 |
10 | 25 | 1968 |
12 | 11 | 1968 |
12 | 19 | 1968 |
<- cbind(birthweight, split_MMDDYYYY(birthweight$birth.date)) birthweight
The cbind()
function combines objects by column. Now that we have the birth month in its own column, we can get the mean of birth weight by month.
tapply(birthweight$birthweight, birthweight$month, mean)
## 1 2 3 4 5 6 7 8
## 3.215000 3.600000 2.933333 3.062500 2.920000 3.273333 3.105000 3.630000
## 9 10 11 12
## 2.965000 3.395000 3.410000 4.035000
5.5 Exercise 3: summarizing the data
Pick one or more of the following questions to answer, or come up with one of your own. Work together. Once you’ve answered a question in one way, can you come up with alternate code that generates the same answer?
- Are preterm babies more likely to have low birth weight?
- What is the ratio of maternal cigarettes to paternal cigarettes for births at each of the hospitals?
- Do taller mothers have taller partners? Do they have longer babies?
5.6 Merging data frames
For this example experiment, we also have placental miRNA expression data. These values are not from real samples, but have been manipulated to roughly reproduce the effect observed by Maccani et al. 2010.
download.file("https://raw.githubusercontent.com/ucdavis-bioinformatics-training/2022_February_Introduction_to_R_for_Bioinformatics/main/miRNA.csv", "miRNA.csv")
<- read.csv("miRNA.csv", row.names = 1) mir
mir
sample.27 | sample.1522 | sample.569 | sample.365 | sample.1369 | sample.1023 | sample.1272 | sample.1262 | sample.575 | sample.792 | sample.752 | sample.619 | sample.1764 | sample.516 | sample.272 | sample.1388 | sample.1363 | sample.300 | sample.57 | sample.431 | sample.532 | sample.223 | sample.1107 | sample.697 | sample.1683 | sample.808 | sample.822 | sample.1016 | sample.820 | sample.1081 | sample.321 | sample.1636 | sample.1360 | sample.1058 | sample.755 | sample.462 | sample.1088 | sample.553 | sample.1191 | sample.1313 | sample.1600 | sample.1187 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
miR-16 | 46 | 56 | 47 | 54 | 56 | 59 | 49 | 55 | 62 | 63 | 46 | 52 | 46 | 61 | 49 | 46 | 61 | 60 | 46 | 70 | 60 | 60 | 57 | 68 | 49 | 59 | 54 | 69 | 58 | 55 | 68 | 63 | 70 | 77 | 56 | 65 | 42 | 63 | 66 | 64 | 50 | 57 |
miR-21 | 52 | 43 | 40 | 35 | 59 | 47 | 42 | 45 | 55 | 45 | 42 | 43 | 40 | 51 | 43 | 44 | 47 | 48 | 39 | 51 | 44 | 46 | 49 | 47 | 48 | 56 | 52 | 41 | 55 | 52 | 46 | 39 | 57 | 55 | 46 | 58 | 54 | 54 | 48 | 47 | 44 | 46 |
miR-146a | 98 | 97 | 87 | 96 | 84 | 96 | 88 | 97 | 96 | 104 | 103 | 92 | 98 | 97 | 91 | 105 | 77 | 89 | 105 | 84 | 94 | 87 | 116 | 98 | 98 | 101 | 86 | 98 | 102 | 93 | 125 | 104 | 111 | 124 | 101 | 101 | 107 | 106 | 102 | 104 | 111 | 86 |
miR-182 | 53 | 45 | 63 | 41 | 46 | 50 | 49 | 50 | 62 | 51 | 64 | 58 | 57 | 59 | 55 | 60 | 60 | 65 | 40 | 48 | 49 | 52 | 48 | 57 | 55 | 74 | 49 | 51 | 53 | 52 | 60 | 43 | 46 | 56 | 50 | 60 | 63 | 60 | 50 | 42 | 67 | 43 |
In this object, the rows are the gene identifiers, and the columns are the samples.
<- as.data.frame(t(mir))
mir $ID <- gsub("sample.", "", rownames(mir))
mir<- merge(birthweight, mir) experiment
What did these lines of code do? Explore each of them, looking up the function help using ?
as necessary.
experiment
ID | birth.date | location | length | birthweight | head.circumference | weeks.gestation | smoker | maternal.age | maternal.cigarettes | maternal.height | maternal.prepregnant.weight | paternal.age | paternal.education | paternal.cigarettes | paternal.height | low.birthweight | geriatric.pregnancy | month | day | year | miR-16 | miR-21 | miR-146a | miR-182 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
27 | 3/9/1967 | Silver Hill | 53 | 3.55 | 37 | 41 | TRUE | 37 | 25 | 161 | 66 | 46 | NA | 0 | 175 | FALSE | TRUE | 3 | 9 | 1967 | 46 | 52 | 98 | 53 |
57 | 8/12/1968 | Memorial | 51 | 3.32 | 38 | 39 | TRUE | 23 | 17 | 157 | 48 | NA | NA | NA | NA | FALSE | FALSE | 8 | 12 | 1968 | 46 | 39 | 105 | 40 |
223 | 12/11/1968 | General | 50 | 3.87 | 33 | 45 | TRUE | 28 | 25 | 163 | 54 | 30 | 16 | 0 | 183 | FALSE | FALSE | 12 | 11 | 1968 | 60 | 46 | 87 | 52 |
272 | 1/10/1968 | Memorial | 52 | 3.86 | 36 | 39 | TRUE | 30 | 25 | 170 | 78 | 40 | 16 | 50 | 178 | FALSE | FALSE | 1 | 10 | 1968 | 49 | 43 | 91 | 55 |
300 | 7/18/1968 | Silver Hill | 46 | 2.05 | 32 | 35 | TRUE | 41 | 7 | 166 | 57 | 37 | 14 | 25 | 173 | TRUE | TRUE | 7 | 18 | 1968 | 60 | 48 | 89 | 65 |
321 | 1/21/1968 | Silver Hill | 48 | 3.11 | 33 | 37 | FALSE | 28 | 0 | 158 | 54 | 39 | 10 | 0 | 171 | FALSE | FALSE | 1 | 21 | 1968 | 68 | 46 | 125 | 60 |
365 | 4/23/1967 | Memorial | 52 | 3.53 | 37 | 40 | TRUE | 26 | 25 | 170 | 62 | 30 | 10 | 25 | 181 | FALSE | FALSE | 4 | 23 | 1967 | 54 | 35 | 96 | 41 |
431 | 9/16/1968 | Silver Hill | 48 | 1.92 | 30 | 33 | TRUE | 20 | 7 | 161 | 50 | 20 | 10 | 35 | 180 | TRUE | FALSE | 9 | 16 | 1968 | 70 | 51 | 84 | 48 |
462 | 6/19/1968 | Silver Hill | 58 | 4.10 | 39 | 41 | FALSE | 35 | 0 | 172 | 58 | 31 | 16 | 25 | 185 | FALSE | TRUE | 6 | 19 | 1968 | 65 | 58 | 101 | 60 |
516 | 1/8/1968 | Silver Hill | 47 | 2.66 | 33 | 35 | TRUE | 20 | 35 | 170 | 57 | 23 | 12 | 50 | 186 | TRUE | FALSE | 1 | 8 | 1968 | 61 | 51 | 97 | 59 |
532 | 10/25/1968 | General | 53 | 3.59 | 34 | 40 | TRUE | 31 | 12 | 163 | 49 | 41 | 12 | 50 | 191 | FALSE | FALSE | 10 | 25 | 1968 | 60 | 44 | 94 | 49 |
553 | 8/17/1968 | Silver Hill | 54 | 3.94 | 37 | 42 | FALSE | 24 | 0 | 175 | 66 | 30 | 12 | 0 | 184 | FALSE | FALSE | 8 | 17 | 1968 | 63 | 54 | 106 | 60 |
569 | 3/23/1967 | Memorial | 50 | 2.51 | 35 | 39 | TRUE | 22 | 7 | 159 | 52 | 23 | 14 | 25 | NA | TRUE | FALSE | 3 | 23 | 1967 | 47 | 40 | 87 | 63 |
575 | 7/12/1967 | Memorial | 50 | 2.78 | 30 | 37 | TRUE | 19 | 7 | 165 | 60 | 20 | 14 | 0 | 183 | FALSE | FALSE | 7 | 12 | 1967 | 62 | 55 | 96 | 62 |
619 | 11/1/1967 | Memorial | 52 | 3.41 | 33 | 39 | TRUE | 23 | 25 | 181 | 69 | 23 | 16 | 2 | 181 | FALSE | FALSE | 11 | 1 | 1967 | 52 | 43 | 92 | 58 |
697 | 2/6/1967 | Silver Hill | 48 | 3.03 | 35 | 39 | FALSE | 27 | 0 | 162 | 62 | 27 | 14 | 0 | 178 | FALSE | FALSE | 2 | 6 | 1967 | 68 | 47 | 98 | 57 |
752 | 10/19/1967 | General | 49 | 3.32 | 36 | 40 | TRUE | 27 | 12 | 152 | 48 | 37 | 12 | 25 | 170 | FALSE | FALSE | 10 | 19 | 1967 | 46 | 42 | 103 | 64 |
755 | 4/25/1968 | Memorial | 53 | 3.20 | 33 | 41 | FALSE | 21 | 0 | 155 | 55 | 25 | 14 | 25 | 183 | FALSE | FALSE | 4 | 25 | 1968 | 56 | 46 | 101 | 50 |
792 | 9/7/1967 | Memorial | 53 | 3.64 | 38 | 40 | TRUE | 20 | 2 | 170 | 59 | 24 | 12 | 12 | 185 | FALSE | FALSE | 9 | 7 | 1967 | 63 | 45 | 104 | 51 |
808 | 5/5/1967 | Silver Hill | 48 | 2.92 | 33 | 34 | FALSE | 26 | 0 | 167 | 64 | 25 | 12 | 25 | 175 | FALSE | FALSE | 5 | 5 | 1967 | 59 | 56 | 101 | 74 |
820 | 10/7/1967 | General | 52 | 3.77 | 34 | 40 | FALSE | 24 | 0 | 157 | 50 | 31 | 16 | 0 | 173 | FALSE | FALSE | 10 | 7 | 1967 | 58 | 55 | 102 | 53 |
822 | 6/14/1967 | Memorial | 50 | 3.42 | 35 | 38 | FALSE | 20 | 0 | 157 | 48 | 22 | 14 | 0 | 179 | FALSE | FALSE | 6 | 14 | 1967 | 54 | 52 | 86 | 49 |
1016 | 7/13/1967 | Silver Hill | 53 | 4.32 | 36 | 40 | FALSE | 19 | 0 | 171 | 62 | 19 | 12 | 0 | 183 | FALSE | FALSE | 7 | 13 | 1967 | 69 | 41 | 98 | 51 |
1023 | 6/7/1967 | Memorial | 52 | 3.00 | 35 | 38 | TRUE | 30 | 12 | 165 | 64 | 38 | 14 | 50 | 180 | FALSE | FALSE | 6 | 7 | 1967 | 59 | 47 | 96 | 50 |
1058 | 4/24/1968 | Silver Hill | 53 | 3.15 | 34 | 40 | FALSE | 29 | 0 | 167 | 60 | 30 | 16 | NA | 182 | FALSE | FALSE | 4 | 24 | 1968 | 77 | 55 | 124 | 56 |
1081 | 12/14/1967 | Silver Hill | 54 | 3.63 | 38 | 38 | FALSE | 18 | 0 | 172 | 50 | 20 | 12 | 7 | 172 | FALSE | FALSE | 12 | 14 | 1967 | 55 | 52 | 93 | 52 |
1088 | 7/24/1968 | General | 51 | 3.27 | 36 | 40 | FALSE | 24 | 0 | 168 | 53 | 29 | 16 | 0 | 181 | FALSE | FALSE | 7 | 24 | 1968 | 42 | 54 | 107 | 63 |
1107 | 1/25/1967 | General | 52 | 3.23 | 36 | 38 | FALSE | 31 | 0 | 164 | 57 | NA | NA | NA | NA | FALSE | FALSE | 1 | 25 | 1967 | 57 | 49 | 116 | 48 |
1187 | 12/19/1968 | Silver Hill | 53 | 4.07 | 38 | 44 | FALSE | 20 | 0 | 174 | 68 | 26 | 14 | 25 | 189 | FALSE | FALSE | 12 | 19 | 1968 | 57 | 46 | 86 | 43 |
1191 | 9/7/1968 | General | 53 | 3.65 | 33 | 42 | FALSE | 21 | 0 | 165 | 61 | 21 | 10 | 25 | 185 | FALSE | FALSE | 9 | 7 | 1968 | 66 | 48 | 102 | 50 |
1262 | 6/25/1967 | Silver Hill | 53 | 3.19 | 34 | 41 | TRUE | 27 | 35 | 163 | 51 | 31 | 16 | 25 | 185 | FALSE | FALSE | 6 | 25 | 1967 | 55 | 45 | 97 | 50 |
1272 | 6/20/1967 | Memorial | 53 | 2.75 | 32 | 40 | TRUE | 37 | 50 | 168 | 61 | 31 | 16 | 0 | 173 | FALSE | TRUE | 6 | 20 | 1967 | 49 | 42 | 88 | 49 |
1313 | 9/27/1968 | Silver Hill | 43 | 2.65 | 32 | 33 | FALSE | 24 | 0 | 149 | 45 | 26 | 16 | 0 | 169 | TRUE | FALSE | 9 | 27 | 1968 | 64 | 47 | 104 | 42 |
1360 | 2/16/1968 | General | 56 | 4.55 | 34 | 44 | FALSE | 20 | 0 | 162 | 57 | 23 | 10 | 35 | 179 | FALSE | FALSE | 2 | 16 | 1968 | 70 | 57 | 111 | 46 |
1363 | 4/2/1968 | General | 48 | 2.37 | 30 | 37 | TRUE | 20 | 7 | 163 | 47 | 20 | 10 | 35 | 185 | TRUE | FALSE | 4 | 2 | 1968 | 61 | 47 | 77 | 60 |
1369 | 6/4/1967 | Silver Hill | 49 | 3.18 | 34 | 38 | TRUE | 31 | 25 | 162 | 57 | 32 | 16 | 50 | 194 | FALSE | FALSE | 6 | 4 | 1967 | 56 | 59 | 84 | 46 |
1388 | 2/22/1968 | Memorial | 51 | 3.14 | 33 | 41 | TRUE | 22 | 7 | 160 | 53 | 24 | 16 | 12 | 176 | FALSE | FALSE | 2 | 22 | 1968 | 46 | 44 | 105 | 60 |
1522 | 3/13/1967 | Memorial | 50 | 2.74 | 33 | 39 | TRUE | 21 | 17 | 156 | 53 | 24 | 12 | 7 | 179 | FALSE | FALSE | 3 | 13 | 1967 | 56 | 43 | 97 | 45 |
1600 | 10/9/1968 | General | 53 | 2.90 | 34 | 39 | FALSE | 19 | 0 | 165 | 57 | NA | NA | NA | NA | FALSE | FALSE | 10 | 9 | 1968 | 50 | 44 | 111 | 67 |
1636 | 2/2/1968 | Silver Hill | 51 | 3.93 | 38 | 38 | FALSE | 29 | 0 | 165 | 61 | NA | NA | NA | NA | FALSE | FALSE | 2 | 2 | 1968 | 63 | 39 | 104 | 43 |
1683 | 2/14/1967 | Silver Hill | 53 | 3.35 | 33 | 41 | FALSE | 27 | 0 | 164 | 62 | 37 | 14 | 0 | 170 | FALSE | FALSE | 2 | 14 | 1967 | 49 | 48 | 98 | 55 |
1764 | 12/7/1967 | Silver Hill | 58 | 4.57 | 39 | 41 | TRUE | 32 | 12 | 173 | 70 | 38 | 14 | 25 | 180 | FALSE | FALSE | 12 | 7 | 1967 | 46 | 40 | 98 | 57 |