【问题标题】:I want to count entries in each column of dataframe [duplicate]我想计算数据框每一列中的条目[重复]
【发布时间】:2019-07-09 18:49:20
【问题描述】:

我目前正在了解将 R 用于数据清理和整理目的。我有一个如下所示的数据框:

Id    Shopping_date_1    Shopping_date_2    Shopping_date_3
1     01/05/2018         01/25/2018         NA
2     02/06/2019         NA                 NA
3     08/05/2018         NA                 01/04/2019

我想安排数据框,以便我得到一个可以计算用户购物次数的列:

Id    Shopping_date_1    Shopping_date_2    Shopping_date_3    Shop_count
1     01/05/2018         01/25/2018         NA                 2
2     02/06/2019         NA                 NA                 1
3     08/05/2018         NA                 01/04/2019         2

请帮忙!

【问题讨论】:

标签: r


【解决方案1】:

你可以这样做:

rowSums(!is.na(df[, 2:length(df)]))

[1] 2 1 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2017-02-16
    • 2010-11-15
    • 2016-10-14
    • 1970-01-01
    • 2015-07-10
    相关资源
    最近更新 更多