Re: [R] how to select the last non-'NA' observation in a row

2010-04-20 Thread Dimitris Rizopoulos
one way is the following: a1 <- c(9,8,rep(NA,5)) a2 <- c(3,NA,3,NA,3,NA,4) a3 <- c(11,6,7,NA,5,NA,6) M <- rbind(a1,a2,a3) ind <- !is.na(M) tapply(M[ind], row(M)[ind], tail, 1) I hope it helps. Best, Dimitris On 4/20/2010 10:33 AM, gallon li wrote: I have a matrix of the following form:

[R] how to select the last non-'NA' observation in a row

2010-04-20 Thread gallon li
I have a matrix of the following form: time id0 2 4 6 9 12 14 3 9 8 NA NA NA NA NA 7 3 NA 3 NA 3 NA 4 13 11 6 7 NA 5 NA 6 . I hope for each row to select the last observation which is not 'NA'. For example, for the first row, id=3, the value I want to select is