【问题标题】:How can I derive a variable in R showing the number of observations that have the same value recorded at earlier dates?如何在 R 中导出一个变量,显示在较早日期记录的具有相同值的观测值的数量?
【发布时间】:2012-08-11 00:49:13
【问题描述】:

我正在使用 R,并且我有一个数据框,其中包含有关个人申请拨款的信息。个人可以根据需要多次申请补助金。我想派生一个新变量,它告诉我每个人已经完成了多少申请,包括每条记录所代表的申请日期。

目前我的数据如下所示:

app number  date app made     applicant
1           2012-08-01        John
2           2012-08-02        John
3           2012-08-02        Jane
4           2012-08-04        John
5           2012-08-08        Alice
6           2012-08-09        Alice
7           2012-08-09        Jane

我想再添加一个变量,所以我的数据框如下所示:

app number  date app made    applicant  applications by applicant to date
1           2012-08-01       John       1
2           2012-08-02       John       2
3           2012-08-02       Jane       1
4           2012-08-04       John       3
5           2012-08-08       Alice      1
6           2012-08-09       Alice      2
7           2012-08-09       Jane       2

我是 R 新手,我真的很难弄清楚如何做到这一点。我能得到的最接近的是这个问题的答案: How do I count the number of observations at given intervals in R?

但我无法根据每条记录中的日期而不是预设的时间间隔计算出如何执行此操作。

【问题讨论】:

    标签: r


    【解决方案1】:

    这是一种比 @Justin 更不优雅的方式:

        A <- read.table(text='"app number"  "date app made"     "applicant"
        1           2012-08-01        John
        2           2012-08-02        John
        3           2012-08-02        Jane
        4           2012-08-04        John
        5           2012-08-08        Alice
        6           2012-08-09        Alice
        7           2012-08-09        Jane',header=TRUE)
    
        # order by applicant name
        A <- A[order(A$applicant), ]
        # get vector you're looking for
        A$app2date <- unlist(sapply(unique(A$applicant),function(x, appl){
                             seq(sum(A$applicant == x))
                           }, appl = A$applicant)
                         )
        # back in original order:
        A   <- A[order(A$"app.number"), ]
    

    【讨论】:

    • +1 因为它看起来与请求的输出完全一样,并且不需要任何额外的包。
    【解决方案2】:

    您可以为此使用plyr。如果您的数据在 data.frame dat 中,我会添加一个名为 count 的列,然后使用 cumsum

    library(plyr)
    dat <- structure(list(number = 1:7, date = c("2012-08-01", "2012-08-02", 
    "2012-08-02", "2012-08-04", "2012-08-08", "2012-08-09", "2012-08-09"
    ), name = c("John", "John", "Jane", "John", "Alice", "Alice", 
    "Jane")), .Names = c("number", "date", "name"), row.names = c(NA, 
    -7L), class = "data.frame")
    
    dat$count <- 1
    
    ddply(dat, .(name), transform, count=cumsum(count))
    
      number       date  name count
    1      5 2012-08-08 Alice     1
    2      6 2012-08-09 Alice     2
    3      3 2012-08-02  Jane     1
    4      7 2012-08-09  Jane     2
    5      1 2012-08-01  John     1
    6      2 2012-08-02  John     2
    7      4 2012-08-04  John     3
    > 
    

    我假设您的日期已经排序,但是您可能希望在“计数”之前明确地对它们进行排序:

    dat <- dat[order(dat$date),]
    

    根据评论,如果您了解(我没有!)transform 的工作方式,这可以简化:

    ddply(dat, .(name), transform, count=order(date))
      number       date  name count
    1      5 2012-08-08 Alice     1
    2      6 2012-08-09 Alice     2
    3      3 2012-08-02  Jane     1
    4      7 2012-08-09  Jane     2
    5      1 2012-08-01  John     1
    6      2 2012-08-02  John     2
    7      4 2012-08-04  John     3
    

    【讨论】:

    • 也许可以通过使用 order() 作为列更简单一些,例如ddply(foo,.(name),transform,count=order(date)。那么就不需要先加列,也不需要单独排序。
    • 知道什么。我没想到变换会隐含“计数”行!编辑得当。
    【解决方案3】:

    这是使用ave 函数的单行方法。此版本不需要对数据重新排序,而是按照原来的顺序保留数据:

    A$applications <- ave(A$app.number, A$applicant, FUN=seq_along)
    

    【讨论】:

    • 谢谢 Greg,我认为这是前进的方向,尽管我会努力尝试理解它们。
    猜你喜欢
    • 2016-01-06
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多