Split up a dataframe by number of NAs in each row

Viewed 93

Consider a dataframe made up of thousand rows and columns that inclues several NAs. I'd like to split this dataframe up into smaller ones based on the number of NAs in each row. All rows that contain the same number of NAs, if there is any, should be in the same group. The new data frames are then saved separately.

> DF
ID  C1 C2 C3 C4 C5
aa  12 13 10 NA 12 
ff  12 NA NA 23 13
ee  67 23 NA NA 21
jj  31 14 NA 41 11
ss  NA 15 11 12 11

The desired output will be:

> DF_chunk_1
ID  C1 C2 C3 C4 C5
aa  12 13 10 NA 12
jj  31 14 NA 41 11
ss  NA 15 11 12 11

> DF_chunk_2
ID  C1 C2 C3 C4 C5
ff  12 NA NA 23 13
ee  67 23 NA NA 21

I appreciate any suggestion.

1 Answers

Try this following useful comments. You can split() and use apply() to build a group:

#Code
new <- split(DF,apply(DF[,-1],1,function(x)sum(is.na(x))))

Output:

$`1`
  ID C1 C2 C3 C4 C5
1 aa 12 13 10 NA 12
4 jj 31 14 NA 41 11
5 ss NA 15 11 12 11

$`2`
  ID C1 C2 C3 C4 C5
2 ff 12 NA NA 23 13
3 ee 67 23 NA NA 21

A more practical way (Many thanks and credits to @RuiBarradas):

#Code2
new <- split(DF, rowSums(is.na(DF[-1])))

Same output.

Related