- X1, X2 NA, :
foo <- function(x) {
if(all(nas <- is.na(x))) {
NA
} else {
x[!nas]
}
}
We use the function foo, applying it to each row of your data (here I have data in an object with a name dat):
> apply(dat, 1, foo)
[1] 1 2 NA
So this gives us what we want. To enable this inside your object, we do the following:
> dat <- within(dat, X3 <- apply(dat, 1, foo))
> dat
X1 X2 X3
1 1 NA 1
2 NA 2 2
3 NA NA NA
source
share