[R] Finding NAs in DF

Johannes Graumann johannes_graumann at web.de
Mon Jan 17 12:23:47 CET 2011


Both versions do not do what I am looking for, as they do not differentiate 
where the NA is, if there is just one.
My original wished for result therefore holts, but should probably be 
rewritten
	c(NA,"B","AB","A")

Joh

On Monday 17 January 2011 14:06:30 Patrick Burns wrote:
> Simpler would be:
> 
> rowSums(is.na(df))
> 
> On 17/01/2011 10:13, Ivan Calandra wrote:
> > 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110117/2628ab1a/attachment.bin>


More information about the R-help mailing list