data.table方法将新值中的值粘贴到另一列中的值上

时间:2017-02-08 11:06:35

标签: r data.table

有人可以建议怎么做吗?我有这些数据:

ty<-data.table(id=c(1,1,1,2,2,3,3,4,4,5,5),
           date=c("11-23-2015","06-22-2015","07-22-2016","03-03-2012","11-08-2015","11-10-2012","11-23-2015","11-12-2012","12-02-2015","08-24-2016","12-25-2008"),
           event=c("open","gt","gf","gf","open","ff","open","open","close","tr","ww"))
ty$date<-as.Date(ty$date,format="%m-%d-%Y")

        id       date event
     1:  1 2015-11-23  open
     2:  1 2015-06-22    gt
     3:  1 2016-07-22    gf
     4:  2 2012-03-03    gf
     5:  2 2015-11-08  open
     6:  3 2012-11-10    ff
     7:  3 2015-11-23  open
     8:  4 2012-11-12  open
     9:  4 2015-12-02 close
    10:  5 2016-08-24    tr
    11:  5 2008-12-25    ww

我需要提取与“open”事件对应的日期,并将其粘贴到按id索引的第四列中,如下例所示:

        id       date event open_date
 1:  1 2015-11-23  open      2015-11-23
 2:  1 2015-06-22    gt      2015-11-23
 3:  1 2016-07-22    gf      2015-11-23
 4:  2 2012-03-03    gf      2015-11-08
 5:  2 2015-11-08  open      2015-11-08
 6:  3 2012-11-10    ff      2015-11-23
 7:  3 2015-11-23  open      2015-11-23
 8:  4 2012-11-12  open      2012-11-12
 9:  4 2015-12-02 close      2012-11-12
10:  5 2016-08-24    tr      NA
11:  5 2008-12-25    ww      NA

我用过这个:

ty[, open_date := if (event == "open") paste(date), by=id]

但它只在参考行中粘贴日期,而我需要在所有id行中粘贴日期。可能不是最佳,但这是我需要的。 谢谢和问候

2 个答案:

答案 0 :(得分:3)

在社区维基回答中转换@Sotos和@DavidArenburg在评论中提供的解决方案:

# @Sotos' solution
ty[, open_date := date[event == 'open'], by = id]
# @DavidArenburg's solution:
ty[ty[event == "open"], open_date := i.date, on = .(id)]

两种解决方案的结果:

#     id       date event  open_date
#  1:  1 2015-11-23  open 2015-11-23
#  2:  1 2015-06-22    gt 2015-11-23
#  3:  1 2016-07-22    gf 2015-11-23
#  4:  2 2012-03-03    gf 2015-11-08
#  5:  2 2015-11-08  open 2015-11-08
#  6:  3 2012-11-10    ff 2015-11-23
#  7:  3 2015-11-23  open 2015-11-23
#  8:  4 2012-11-12  open 2012-11-12
#  9:  4 2015-12-02 close 2012-11-12
# 10:  5 2016-08-24    tr       <NA>
# 11:  5 2008-12-25    ww       <NA>

答案 1 :(得分:1)

我们按'id'分组,创建'event'(event=="open")的逻辑向量,将与之对应的'date'子集并将其分配给crate'open_date'

ty[,open_date:= date[event=="open"][1L] , by = id]

或另一个选项是match

ty[, open_date := date[match('open', event)], by = id]

注意:假设每个'id'只有一个'open'