Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

r - Split up a dataframe by number of NAs in each row

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.

question from:https://stackoverflow.com/questions/65848839/split-up-a-dataframe-by-number-of-nas-in-each-row

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...