【问题标题】:How to get number and names of columns that have at least one null value如何获取至少具有一个空值的列的数量和名称
【发布时间】:2016-01-27 10:00:30
【问题描述】:

在获取列数和名称后,我想过滤掉至少有一个缺失值的列。 我使用此函数获取仅包含缺失值的列的名称和数量,而不是将它们从数据框中过滤掉:

CheckColAllNulls <- if (ncol(Filter(function(x) all(is.na(x)), df)) > 0) {
  cat("columns have only nulls:",ncol(Filter( function(x) all(is.na(x)), df)))
  cat("columns names that have only nulls:",colnames(Filter( function(x) all(is.na(x)), df)))
  df <- Filter(function(x) ! all(is.na(x)), df)
  print("columns having only nulls removed ")
} else {
  print("No columns having only nulls are found")
}

我尝试使用colSums 对至少有一个缺失值的列执行相同的操作,但没有成功。

CheckColNulls <- if ( colSums(is.na(df)) > 0) {
  cat("columns have more than one null:",ncol(colSums(is.na(df)) > 0 ))
  cat("columns names that have more than one null:",colnames(colSums(is.na(df)) > 0 ))
  df <- Filter(function(x)  colSums(is.na(x)) > 0), df)
  print("columns having at least one null removed ")
} else {
  print("No columns having at least one null are found")
}

这是我得到的错误:

Error in colSums(is.na(x)) : 
  'x' must be an array of at least two dimensions
In addition: Warning message:
In if (colSums(is.na(df)) > 0) { :
  the condition has length > 1 and only the first element will be used

【问题讨论】:

  • 请在您的语言中更准确。 NANULL 不同。
  • 警告可以被if (c(3,7) &gt; 0) print("hello")转载

标签: r dataframe


【解决方案1】:

这是我使用any(is.na(x)) 的解决方案:

CheckColAllNulls <- if (ncol(Filter(function(x) any(is.na(x)), df)) > 0) {
    cat("columns have only nulls:",ncol(Filter( function(x) any(is.na(x)), df)))
    cat("\n\n")
    cat("columns names that have only 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 any nulls are removed")
} else {
    print("No columns having any nulls are found")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多