Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set new variable NA if any other is NA

Tags:

r

I want to add a new variable (N_notNAs) to my data frame, which defines whether any of the other variables is NA.

x   y   z   N_notNAs
2   3   NA  NA
NA  1   3   NA
2   3   5   1
4   4   3   1
like image 686
SDahm Avatar asked Nov 27 '22 10:11

SDahm


2 Answers

Not sure why this is your desired output, but a possible way to achieve this is summing the NA per row and put it the power of NA- this way NA^0 will return 1, and everything else will become NA

NA^rowSums(is.na(df))
# [1] NA NA  1  1
like image 138
David Arenburg Avatar answered Mar 28 '23 14:03

David Arenburg


@David Arenburg's solution is very nifty but here are a few more anyways.

The first 4 assume that the columns of df are numeric and use the same basic idea whereas the last 3 do not require numeric columns. They also use a common idea.

The ifelse solution is longest in terms of character count but may be more readable and least tricky.

As mentioned by a @alexis_laz below, apply(...) could be replaced with !complete.cases(df) in the last 3 solutions and with some simplifications we would get ifelse(complete.cases(df), 1, NA), c(NA, 1)[complete.cases(df) + 1] and match(complete.cases(df), TRUE) .

rowSums(0*df) + 1

max.col(0*df) + 1

do.call(pmin, 0*df) + 1

do.call(pmax, 0*df) + 1

ifelse(apply(df, 1, anyNA), NA, 1)

c(NA, 1)[apply(df, 1, anyNA) + 1]

match(apply(df, 1, anyNA), FALSE)

There is some question of why you want a column that is NA or 1. If you wanted a logical TRUE/FALSE result then apply(df, 1, anyNA) would do.

like image 40
G. Grothendieck Avatar answered Mar 28 '23 16:03

G. Grothendieck