Вы должны попробовать использовать функцию, созданную пользователем. Вот тот, который я придумал:
library(tidyverse)
test_function <- function(vector){
##The ifelse returns TRUE if the element in the vector is NA, NULL, or ""
x <- ifelse(is.na(vector)|vector == ""|is.null(vector), TRUE, FALSE)
##Returns the sum of boolean vector (FALSE = 0, TRUE = 1)
return(sum(x))
}
Чтобы применить функцию к кадру данных, вы можете использовать любую из функций применения, но я рекомендую sapply, поскольку она возвращает вектор.
##Create a data frame with mock data
test_df <- tibble(x = c(NA, NA, NA, "","",1,2,3),
y = c(NA, "","","","","","",1),
z = c(0,0,0,0,0,0,0,0))
##Assign the result to a new variable
total_missing_by_column <- sapply(test_df, test_function)
##You can also build a data frame with the variables and the total missing
tibble(variable = colnames(test_df),
total_missing = sapply(test_df, test_function))
Надеюсь, это поможет