I am trying to get the column name and number of columns that have "NA"
in them, I have tried using this code
names(df)[sapply(df, anyNA)]
but it only gives me the column names and no the numbers, any idea how to get an output for both?
CodePudding user response:
We may convert the logical vector to index with which
and subset the index with names
to get the column names
i1 <- sapply(df, anyNA)
which(i1)
names(df)[i1]
We may not need the names(df)[i1]
as which
gives a named vector of index though i.e
which(sapply(df, anyNA))
will be a single line code to give both column names and index
Or with dplyr
library(dplyr)
df %>%
summarise(across(where(anyNA), ~ match(cur_column(), names(df))))
CodePudding user response:
Using which
with colsums
of NA
.
which(colSums(is.na(iris_na)) > 0)
# Sepal.Length Petal.Length
# 1 3
Data:
iris_na <- iris
iris_na[c(1, 3)] <- lapply(iris_na[c(1, 3)], \(x) replace(x, sample(length(x), length(x)/10), NA_real_))