[R] subsetting

John Kane jrkrideau at yahoo.ca
Thu Oct 25 19:26:32 CEST 2007


I don't understand the criteria that gives you just
the three ids.  

As I read the criteria if we put the data into a
data.frame you have 

subset(mydata ,year==year.hatch & year.1st.reprod ==
year.hatch+1)

but this gives more than the three ids.  What am I
missing?

--- henrik.parn at bio.ntnu.no wrote:

> 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



More information about the R-help mailing list