[R] subsetting
henrik.parn at bio.ntnu.no
henrik.parn at bio.ntnu.no
Thu Oct 25 16:15:22 CEST 2007
Dear all,
I have received some data on birds that looks sth like this:
# a unique id for each individual
id <- c(1,1,1,2,2,2,3,3,3,4,4,5,6)
# the year the bird was measured
year <- c(1995, 1996, 1997, 1995, 1996, 1997, 1996, 1997, 1998, 1996, 1997, 1997, 1998)
# the year the bird was hatched
year.hatch <- c(1995, 1995, 1995, 1995, 1995, 1995, 1996, 1996, 1996, 1996, 1996, 1997, 1998)
# the year when the bird reproduced the first time (for simplicity in the dummy data, all birds reproduce the year after it was born)
year.1st.reprod <- year.hatch + 1
# some status (0/1) of an individual
status <- c(1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1)
# the id of the mother
id.mother <- c(NA, NA, NA, NA, NA, NA, 1, 1, 1, 2, 2, 3, 3)
# Status of the mother (0/1)
status.mother <- c(NA, NA, NA, NA, NA, NA, 1, 1, 1, 0, 0, 1, 1)
# a measure of individual
measure <- rnorm(13)
test.data <- data.frame(id, year, year.hatch, year.1st.reprod, status, id.mother, status.mother, measure)
test.data
--------------------------------
I want to compare measures of individuals that have mothers of different status.
But I need help to make the following subset of individuals:
I want the measures that are taken on individuals the year they are hatched. So for the individuals I compare, I want year==year.hatch.
...and
I also want that the individuals from which I obtain measurements are offspring from the first reproduction year of their parents.
So for the individuals I compare, I want their /mothers/ to have year==year.1st.reproduction.
Applying my selection criteria on the simple dummy data above, I would end up with the measures from id=3 in 1996, id=4 in 1996 and id=5 in 1997.
Can anyone help me how to perform this subsetting in R?
Thanks a lot in advance!
Henrik
--
************************
Henrik Pärn
Department of Biology
Norwegian University of Science and Technology
NO-7491 Trondheim
Norge
+47 735 96285 (office)
+47 909 89 255 (mobile)
+47 735 96100 (fax)
More information about the R-help
mailing list