[R] Finding NAs in DF

jim holtman jholtman at gmail.com
Mon Jan 17 14:14:21 CET 2011


building on the previous responses, does this give you what you want:

> x
   A  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?



More information about the R-help mailing list