对具有多个变化条件的行求和



我试图在data.framedata.table中创建两个条件的列。我所看到的帖子的不同之处在于,我试图在下面修改的是,我对条件没有"价值",但条件取决于data.frame中的其他变量。

让我们假设这是我的数据帧:

mydf <- data.frame (Year = c(2000, 2001, 2002, 2004, 2005,
                             2007, 2000, 2001, 2002, 2003,
                             2003, 2004, 2005, 2006, 2006, 2007),
                    Name = c("Tom", "Tom", "Tom", "Fred", "Gill",
                             "Fred", "Gill", "Gill", "Tom", "Tom",
                             "Fred", "Fred", "Gill", "Fred", "Gill", "Gill"))

我想知道在过去的5年里,这3名受试者经历了多少次相同的事件。但是,如果事件的日期超过5年,我不想包括它。我认为我可以对一个指标变量求和(如果主体在一年中经历了该事件,则将其设置为1),同时按照Year < Year & Year >= Year-5的方式指定一些东西。所以基本上是把小于焦点年和大于或等于焦点年前5年的经验加起来。

我已经创建了一个求和指示器和一个焦点年- 5的变量

mydf$Ind <- 1
mydf$Yearm5 <- mydf$Year-5

然后我转换到数据表的速度(原来的df有+60k obs)

library(data.table)
mydf <- data.table(mydf)

现在的问题是我不能让这两个条件工作。我所看到的帖子似乎都知道一个特定的值来子集(例如R数据)。),但在我的情况下,值随着观察值的变化而变化(不确定这是否意味着我需要做一些循环?)。

我想我需要一些类似这样的东西:

mydf[, c("Exp"):= sum(Ind), by = c("Name")][Year < Year & Year >= Yearm5]

给:

Empty data.table (0 rows) of 5 cols: Year,Name,Ind,Yearm5,Exp

只使用一个条件

mydf1 <- mydf[, c("Exp"):= sum(Ind), by = c("Name")][Year >= Yearm5] 

给出了总经验,所以我假设Year < Year条件有问题。

我不太确定是什么。我也试着修改了以下的建议:如何累计添加值在一个向量在R运气不佳,我指定条件的方式似乎出了问题。

library(dplyr)
mytest1 <- mydf %>%
           group_by(Name, Year) %>%
           filter(Year < Year & Year >= Yearm5) %>%
           mutate(Exp = sum(Ind))

结果如下:

myresult <- data.frame (Year = c(2003, 2004, 2004, 2006,
                                 2007, 2000, 2001, 2005,
                                 2005, 2006, 2007, 2000,
                                 2001, 2002, 2002, 2003),
                        Name = c("Fred", "Fred", "Fred", "Fred",
                                 "Fred", "Gill", "Gill", "Gill",
                                 "Gill", "Gill", "Gill", "Tom",
                                 "Tom", "Tom", "Tom", "Tom"),
                        Ind = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
                        Exp = c(0, 1, 1, 3, 4, 0, 1, 1, 1, 2, 3, 0, 1, 2, 2, 4),
                        Yearm5 = c(1998, 1999, 1999, 2001, 2002,
                                   1995, 1996, 2000, 2000, 2001,
                                   2002, 1995, 1996, 1996, 1997, 1998))

任何帮助或指针将不胜感激!

这里有一个更data.table的方法,使用roll

setDT(mydf)
# this is our desired end point
boundary = mydf[, list(Name, year.end = Year + 4)]
# set the key for the following merges
setkey(mydf, Name, Year)
setkey(boundary, Name, year.end)
# add indices that will keep track of the positions to compute deltas
mydf[, idx := .I]
boundary[, idx := .I]
# merge, rolling to match the end correctly, and then subtract the indices
# to get the desired delta.
# Note that we need to unique data because of duplicates.
# Depending on data you may also need to add `allow.cartesian = TRUE`.
# Final note - in data.table <= 1.9.2 you should omit the `by = .EACHI` part.
mydf[unique(boundary)[unique(mydf), list(Exp = i.idx - idx),
                      roll = -Inf, by = .EACHI]]
#    Year Name idx Exp
# 1: 2003 Fred   1   0
# 2: 2004 Fred   2   1
# 3: 2004 Fred   3   1
# 4: 2006 Fred   4   3
# 5: 2007 Fred   5   4
# 6: 2000 Gill   6   0
# 7: 2001 Gill   7   1
# 8: 2005 Gill   8   1
# 9: 2005 Gill   9   1
#10: 2006 Gill  10   2
#11: 2007 Gill  11   3
#12: 2000  Tom  12   0
#13: 2001  Tom  13   1
#14: 2002  Tom  14   2
#15: 2002  Tom  15   2
#16: 2003  Tom  16   4

下面是使用rollapplydata.table的方法

library(zoo)
 setDT(mydf)
 setkey(mydf, Name,Year)
 # create a data.table that has all Years and incidences including the 5 year window 
 # and sum up the number of incidences per year for each subject 
m <- mydf[CJ(unique(Name),seq(min(Year)-5, max(Year))),allow.cartesian=TRUE][,
            list(Ind = unique(Ind), I2 = sum(Ind,na.rm=TRUE)),
            keyby=list(Name,Year)]
# use rollapply over this larger data.table to get the number of
# incidences in the previous 5 years (not including this year (hence head(x,-1))
m[,Exp := rollapply(I2, 5, function(x) sum(head(x,-1)), 
                    align = 'right', fill=0),by=Name]
# join with the original to create your required data
m[mydf, !'I2']
   Name Year Ind Exp
#  1: Fred 2003   1   0
#  2: Fred 2004   1   1
#  3: Fred 2004   1   1
#  4: Fred 2006   1   3
#  5: Fred 2007   1   4
#  6: Gill 2000   1   0
#  7: Gill 2001   1   1
#  8: Gill 2005   1   1
#  9: Gill 2005   1   1
# 10: Gill 2006   1   2
# 11: Gill 2007   1   3
# 12:  Tom 2000   1   0
# 13:  Tom 2001   1   1
# 14:  Tom 2002   1   2
# 15:  Tom 2002   1   2
# 16:  Tom 2003   1   4

data.table,我认为你正在寻找的语法应该是这样的:

setDT(mydf)
mydf[ , Exp := rank(x=Year,ties.method="min")-1, by=Name]

相关内容

  • 没有找到相关文章

最新更新