I want to create a vector of column names for columns that have fewer than 3 NAs. Here is my data and what I have tried so far:
df <- data.frame("A" = c(1,3,4,6,NA,3,NA),
"B" = c(2,4,5,6,7,8,9),
"C" = c(3,4,NA,NA,2,3,NA))
NA.list <- map(df, function (x) sum(is.na(x))>=3)
I want to create an output that is similar to this:
names <- c("A", "B")
Any thoughts?
a simple solution
colnames(df[colSums(is.na(df)) < 3])
[1] "A" "B"
Using Filter
:
names(Filter(function(x) x < 3, colSums(is.na(df))))
#[1] "A" "B"
With the same logic you can also use purrr::keep
names(purrr::keep(df, ~sum(is.na(.x)) < 3))
With dplyr
we can use select
library(df)
df %>%
select(where(~ sum(is.na(.)) < 3)) %>%
names
#[1] "A" "B"
Or using base R
names(which(sapply(df, function(x) sum(is.na(x))) < 3))
#[1] "A" "B"
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.