简体   繁体   中英

How to remove columns with dplyr with NA in specific row?

This code removes all columns which contain at least one NA.

library(dplyr)
df %>%
    select_if(~ !any(is.na(.)))

What do I need to modify if I want only remove the columns that have NA for the eighth row (for my generated data below)?

set.seed(1234)
df <- data.frame(A = 1:10, B = 11:20, C = 21:30)
df <- as.data.frame(lapply(df, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))

In base-R one can simply try as:

df[,which(!is.na(df[8,]))]

Or as suggested by @RichScriven :

df[, !is.na(df[8,])]

#    A  B
# 1   1 11
# 2   2 12
# 3   3 13
# 4   4 NA
# 5  NA 15
# 6   6 16
# 7   7 17
# 8   8 18
# 9   9 19
# 10 10 20

You could do this:

df %>% 
  select_if(!is.na(.[8,]))

    A  B
1   1 11
2   2 12
3   3 13
4   4 NA
5  NA 15
6   6 16
7   7 17
8   8 18
9   9 19
10 10 20

Another option is keep

library(purrr)
keep(df, ~ !(is.na(.x[8])))
#    A  B
#1   1 11
#2   2 12
#3   3 13
#4   4 NA
#5  NA 15
#6   6 16
#7   7 17
#8   8 18
#9   9 19
#10 10 20

Or with Filter from base R

Filter(function(x) !(is.na(x[8])), df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM