I "think" I understand what you want. This seems to work for the test data you supplied below. At least it gives the expected answer. ================================================ df1 <- unique(test.data[,c(1,4)]) names(df1) <- c("id.mother", "yy"); df1
df2 <- merge(test.data, df1) ; df2 df3 <- subset(df2, df2$year==df2$year.hatch & df2$yy==df2$year.hatch) df3 ================================================= --- [EMAIL PROTECTED] wrote: > Sorry that I was unclear. For an individual to > qualify for my analysis I > want both of the following two criteria to be > fulfilled: > > First, I want to select measurement taken at a > certain age: for the > focal individual the year of measurement (year) > should be the same as > year.hatch > Second, I want the focal individual to be born by a > mother that > reproduces for the first time. > So the /parents /of the focal individual should have > year == > year.1st.reprod. > > The problem for me, I think, is that in my data set > with several > generations, an individual can be both offspring and > parent to other > offspring at the same time (e.g. id=3 in test.data). > In the 'offspring > role', to select a individual from which I want the > measurements, I > would use year==year.hatch. However, for the /same/ > individual in the > 'parent role', to pass the 'parent test', it should > have > year==year.1st.reprod. > > In the test data, > id=3 is an offspring of id=1 in id=1's first > breeding year (OK!). We > then select the measurements from id=1 on the year > it is born, i.e 1996. > id=4 is an offspring of id=2 in id=2's first > breeding year (OK!). We > then select the measurements from id=4 on the year > it is born, i.e 1996. > id=5 is an offspring of id=3 in id=3's first > breeding year (OK!). We > then select the measurements from id=5 on the year > it is born, i.e 1997. > id=6 is an also an offspring of id=3, however /not > /from id=3's first > breeding year (not OK!). > > > Cheers, > > Henrik > > > John Kane wrote: > > >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? > > > >--- [EMAIL PROTECTED] 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? > >> > >> [[replacing trailing spam]] > >> > >> > >>Henrik > >> > >> > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.