[R] Issues with the way Apply handled NA's
S Ellison
S.Ellison at LGCGroup.com
Wed Nov 16 15:57:59 CET 2016
> -----Original Message-----
>
> You can check for an empty vector as follows:
> ...
> vals <- apply(plabor[c("colA","colB","colC")],1,function(x) length(na.omit(x)))
> vals # [1] 3 0 3 2
> <- ifelse(vals>0, plabor$colD, NA)
>
plabor
A slightly more compact variant that avoids the intermediate 'vals' variable is to apply an anonymous function that does the check internally:
plabor$colD <- apply(plabor, 1, function(x) if(all(is.na(x))) NA else prod(x, na.rm=TRUE))
S Ellison
*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If
you have received this message in error, please notify the sender
immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com
and delete this message and any copies from your computer and network.
LGC Limited. Registered in England 2991879.
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
More information about the R-help
mailing list