Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

filter - R Removing row if three or more values are NA

I feel like I should be able to do this with filter or subset, but can't figure out how.

How do I remove a row if three or more of the cells in that row are "NA"?

So in this dataset, rows with titles 1A-C2 and 3A-C2 would be removed.

my_data <- data.frame(Title = c("1A-C2", "1D-T2", "1F-T1", "1E-C2", "3A-C2", "3F-T2"),
                      Group1 = c(NA, 10, 2, 9, NA, 4), Group2 = c(1, 3, 6, 1, NA, 3), Group3=c(NA, 3, 3, 8, NA, 4), Group4=c(NA, NA, 4, 5, 1, 7), Group5=c(1, 4, 3, 3, 9, NA), Group6=c(NA, 4, 5, 6, 1, NA))

Thank you!!

question from:https://stackoverflow.com/questions/65857023/r-removing-row-if-three-or-more-values-are-na

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

1 Answer

0 votes
by (71.8m points)

With Base R,

  my_data[rowSums(is.na(my_data))<3,]

gives,

  Title Group1 Group2 Group3 Group4 Group5 Group6
2 1D-T2     10      3      3     NA      4      4
3 1F-T1      2      6      3      4      3      5
4 1E-C2      9      1      8      5      3      6
6 3F-T2      4      3      4      7     NA     NA

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...