How to get the number of columns with ALL NA
values? Meaning that every value in a column needs to be NA
, for the column to be counted.
Asked
Active
Viewed 49 times
1 Answers
4
You could use colSums
to count NA in the column and check if is the same as all row numbers of the dataframe. Here is a reproducible example where there are 2 columns with all values NA:
sum(colSums(is.na(df)) == nrow(df))
#> [1] 2
Created on 2023-01-06 with reprex v2.0.2
Data:
df <- data.frame(V1 = c(NA, NA, NA), V2 = c(1, 2, NA), V3 = c(NA, NA, NA), V4 = c(2, 2, 1))
df
#> V1 V2 V3 V4
#> 1 NA 1 NA 2
#> 2 NA 2 NA 2
#> 3 NA NA NA 1

Quinten
- 35,235
- 5
- 20
- 53