【问题标题】:How do I count the number of cells for certain columns that have data?如何计算某些包含数据的列的单元格数?
【发布时间】:2016-05-03 17:42:20
【问题描述】:

我有一个名为 df 的数据框,如下所示:

name score1 score2 score3
Joe     1      NA    3
Jane    NA     2     3

如何创建一个TOTAL_NonEmpty 列来计算score1score2score3 中非空单元格的数量?

【问题讨论】:

  • sum(!is.na(df[, c('score1', 'score2', 'score3')]))?
  • df$TOTAL_NonEmpty <- rowSums(!is.na(df[-1]))
  • 是的,我错过了“逐行”部分。很好的解决方案。

标签: r


【解决方案1】:

在基础 R 中,

df$TOTAL_NonEmpty <- rowSums(!is.na(df))

## For specifically those columns:
df$TOTAL_NonEmpty <- rowSums(!is.na(df[, c('score1', 'score2', 'score3')]))

应该给你每行的非缺失值的计数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多