I have a matrix of the following form: time id 0 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 8 for the second row, id=7, the value I want to select is 4 for the third row, id=13, the value I want to select is 6 .... if it would be easier for you to demonstrate for me, here I include the code to generate the above three-row matrix: 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) matrix=rbind(a1,a2,a3) [[alternative HTML version deleted]] ______________________________________________ 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.