Re: [R] select observations from longitudinal data

2009-03-29 Thread Wacek Kusnierczyk
Peter Dalgaard wrote: > >> >> times = 3:4 >> do.call(rbind, by(data, data$id, function(data) >> with(data, { >> rows = (time == times[which(times %in% time)[1]]) >> if (is.na(rows[1])) data.frame(id=id, time=NA, x=NA) else >> data[rows,] }))) >> >> # id

Re: [R] select observations from longitudinal data

2009-03-29 Thread Peter Dalgaard
Wacek Kusnierczyk wrote: gallon li wrote: Suppose I have a long format for a longitudinal data id time x 1 1 10 1 2 11 1 3 23 1 4 23 2 2 12 2 3 13 2 4 14 3 1 11 3 3 15 3 4 18 3 5 21 4 2 22 4 3 27 4 6 29 I want to select the x values for each ID when time is equal to 3. When that observation is

Re: [R] select observations from longitudinal data

2009-03-29 Thread Wacek Kusnierczyk
gallon li wrote: > Suppose I have a long format for a longitudinal data > > id time x > 1 1 10 > 1 2 11 > 1 3 23 > 1 4 23 > 2 2 12 > 2 3 13 > 2 4 14 > 3 1 11 > 3 3 15 > 3 4 18 > 3 5 21 > 4 2 22 > 4 3 27 > 4 6 29 > > I want to select the x values for each ID when time is equal to 3. When that > obse

Re: [R] select observations from longitudinal data

2009-03-29 Thread Bill.Venables
Let's tackle the bigger problem of doing this not just for time = 3 but for all times. First we start with your data frame: > dat id time x 1 11 10 2 12 11 3 13 23 4 14 23 5 22 12 6 23 13 7 24 14 8 31 11 9 33 15 10 34 18 11 35 2

Re: [R] select observations from longitudinal data set

2009-01-18 Thread Gabor Grothendieck
Try this. 'by' splits up the data frame into one data frame per id and then f acts separately on each such sub-dataframe returning a ts series with NAs for the missings. cbind'ing those all together gives us this series with one column per id: > tt Time Series: Start = 1 End = 6 Frequency = 1

Re: [R] select observations from longitudinal data set

2009-01-18 Thread Dimitris Rizopoulos
one way is the following: dat <- read.table(textConnection("id time y 1 1 10 1 2 12 1 3 15 1 6 18 2 1 8 2 3 9 2 4 11 2 5 12 3 1 8 3 4 16 4 1 9 4 5 13 5 1 7 5 2 9 5 6 11"), header = TRUE) closeAllConnections() val <- 4 dat. <- data.frame(id = unique(dat$id), time = val) out <- merge(dat, dat., al