r - How to get number and names of columns that have at least one null value -


i filter out columns have @ least 1 missing value after getting number of columns , thier names. use function names , number of columns contain missing values, filter them out of data frame:

checkcolallnulls <- if (ncol(filter(function(x) all(is.na(x)), df)) > 0) {   cat("columns have nulls:",ncol(filter( function(x) all(is.na(x)), df)))   cat("columns names have nulls:",colnames(filter( function(x) all(is.na(x)), df)))   df <- filter(function(x) ! all(is.na(x)), df)   print("columns having nulls removed ") } else {   print("no columns having nulls found") } 

i tried same operation columns have @ least 1 missing value using colsums out success.

checkcolnulls <- if ( colsums(is.na(df)) > 0) {   cat("columns have more 1 null:",ncol(colsums(is.na(df)) > 0 ))   cat("columns names have more 1 null:",colnames(colsums(is.na(df)) > 0 ))   df <- filter(function(x)  colsums(is.na(x)) > 0), df)   print("columns having @ least 1 null removed ") } else {   print("no columns having @ least 1 null found") } 

here error get:

error in colsums(is.na(x)) :    'x' must array of @ least 2 dimensions in addition: warning message: in if (colsums(is.na(df)) > 0) { :   condition has length > 1 , first element used 

here solution using any(is.na(x)):

checkcolallnulls <- if (ncol(filter(function(x) any(is.na(x)), df)) > 0) {     cat("columns have nulls:",ncol(filter( function(x) any(is.na(x)), df)))     cat("\n\n")     cat("columns names have nulls:",colnames(filter( function(x) any(is.na(x)), df)))     cat("\n\n")     df <- filter(function(x) ! any(is.na(x)), df)      print("columns having nulls removed") } else {     print("no columns having nulls found") } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -