1

I have DF like:

A  B  C  D | Va
NA 2  NA 3 | x
1  2  3  4 | x
NA 5  5  2 | x 
4  3  2  1 | x

It is needed it to be like:

A  B  C  D | Va
1  2  3  4 | x
4  3  2  1 | x

The aim of is to calculate Va value. I have already calculate it by some rules previously (lets name it Vb), but it has low quality. Therefore, I need to calculate Va to get better values, where I can (where there is no NA in row). Then I need to get V vector, where Va calculated for some rows and Vb everywhere else. Illustration of what I need:

Va  Vb  ->  V
NA  2   ->  2
1   2   ->  1
NA  2   ->  2
1   2   ->  1

So, I have 2 questions:

1) How to rescale my original DF to some newDF, where each row has no NAs with respect to original index in DF?

2) I have tried Va[is.na(Va)] <- Vb to test NA replacement with Vb values (low quality model). It worked even with message about different length/number of replacements. Is it ok to use it to get what I need or maybe there is something better?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Bear
  • 338
  • 1
  • 3
  • 11

1 Answers1

1

We can use rowSums and is.na to subset the rows of "DF"

DF[!rowSums(is.na(DF)),]
#   A B C D Va
#2 1 2 3 4  x
#4 4 3 2 1  x
akrun
  • 874,273
  • 37
  • 540
  • 662