Hi, What is an efficient way to take this DF data.frame(A=c(1,2,NA,NA),B=c(1,NA,NA,4)) and get c(NA,"TWO","BOTH","ONE") as the result, where NA corresponds to a row without "NA"s, TWO indicates NA in the second and ONE in the first column. Thanks for any pointers. Joh
Hi, I hope you made a mistake in c(NA,"TWO","BOTH","ONE") because if not, I have no idea what you're looking for... But would that do? df <- data.frame(A=c(1,2,NA,NA),B=c(1,NA,NA,4)) apply(df,1, FUN=function(x) length(x[is.na(x)])) [1] 0 1 2 1 There might be better ways to do it, but it works HTH, Ivan Le 1/17/2011 11:01, Johannes Graumann a ?crit :> Hi, > > What is an efficient way to take this DF > > data.frame(A=c(1,2,NA,NA),B=c(1,NA,NA,4)) > > and get > c(NA,"TWO","BOTH","ONE") > > as the result, where NA corresponds to a row without "NA"s, TWO indicates NA > in the second and ONE in the first column. > > Thanks for any pointers. > > Joh > > ______________________________________________ > R-help at 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
building on the previous responses, does this give you what you want:> xA B 1 1 1 2 2 NA 3 NA NA 4 NA 4> # determine where the NAs are > row.na <- apply(x, 1, is.na) > # now convert to list of columns with NAs > apply(row.na, 2, function(a) paste(colnames(x)[a], collapse = ','))[1] "" "B" "A,B" "A"> >On Mon, Jan 17, 2011 at 5:01 AM, Johannes Graumann <johannes_graumann at web.de> wrote:> Hi, > > What is an efficient way to take this DF > > ? ? ? ?data.frame(A=c(1,2,NA,NA),B=c(1,NA,NA,4)) > > and get > ? ? ? ?c(NA,"TWO","BOTH","ONE") > > as the result, where NA corresponds to a row without "NA"s, TWO indicates NA > in the second and ONE in the first column. > > Thanks for any pointers. > > Joh > > ______________________________________________ > R-help at 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?