【问题标题】:Calculate the number of individual with more than i days of life with data.table in R使用 R 中的 data.table 计算寿命超过 i 天的个体数量
【发布时间】:2021-08-14 15:29:46
【问题描述】:

这是我的简化数据表:

Individual time_alive (day)
ID1 1
ID2 5
ID3 7
ID4 5

我需要计算每天活着的人数。 我通过循环实现了这一点

for (i in c(-1:600)) {
  y<-summarise(DT , time_alive > i )
  Alive[i+2,]<-length(y[y==TRUE])
}

但是,对于超过 2B 个观察值的 data.frame,这确实很长。

我想尝试使用 data.table 的替代方法,但我被困在只有 1 天的有效计算数:

DT[,.N,time_alive> i][time_alive==TRUE,2] 

这里,i 不能用向量代替,只能用 1 个数字代替。我想计算寿命超过 i 天的个体数量,而不做循环。

我对简化数据的预期结果是:

Day Number of individual alive
1 4
2 3
3 3
4 3
5 3
6 1
7 1
8 0

一行中的最佳解决方案,data.table 比循环快得多:

DT[, .(Day = seq_len(1 + max(time_alive)))][DT[,.(time_alive)], .(.N), on = .(Day <= time_alive), by = Day]

【问题讨论】:

  • 很难理解你的问题。您的列 time_alive 中的变量究竟是什么,您期望什么输出?一世。 e.:如果您的数据只有这 4 行,您希望得到什么结果?
  • 我编辑了我的问题,因此您可以使用 data.table 获得我正在寻找的结果。例如,对于变量 time_alive,个体 2 在第 5 天死亡,因此他被认为在第 1 天到第 5 天还活着。

标签: r list data.table cumsum


【解决方案1】:
# @r2evans suggestion about making it a one-liner
# replaced res = data.table('day' = 1:max(DT$time_alive))
DT[, .(day = seq_len(1 + max(time_alive)))][
     # my original solution
     DT, .(.N) ,on = .(day <= time_alive),by = day, allow.cartesian = T]

# or 

DT[,time_alive > TARGET_NUMBER, by = individual]

根据您提供的内容,我有两个解决方案。它们中的一个或两个应该是您正在寻找的。详情/说明见下文

# load in data
DT = data.table('individual' = 1:4, 'time_alive' = c(1,5,7,5))
# set your target number
TARGET_NUMBER = 5

# group by individual, 
# then check if the number of days they were alive is greater than your target
# this answers "i want to calculate the number of 
# individual with more than "i" days of life

DT[,time_alive > TARGET_NUMBER, by = individual]

individual    V1
1:          1 FALSE
2:          2 FALSE
3:          3  TRUE
4:          4 FALSE

# if the result you want is that table you created. that is a little different:
# create a table with days ranging from 1 to the maximum survivor

res = data.table('day' = 1:max(DT$time_alive))

day
1:   1
2:   2
3:   3
4:   4
5:   5
6:   6
7:   7


# use joins
# join by time alive being greater than or equal to the day
# group by the specific day, and count how many observations we have
# allow.cartesian because the mapping isn't one-to-one

res[DT, .(.N) ,on = .(day <= time_alive),by = day, allow.cartesian = T]

day N
1:   1 4
2:   2 3
3:   3 3
4:   4 3
5:   5 3
6:   6 1
7:   7 1

【讨论】:

  • 我喜欢“加入”解决方案,它是一种数据优雅的方法,+1。您可以使用DT[, .(Day = seq_len(1 + max(time_alive)))][DT[,.(time_alive)], .(.N), on = .(Day &lt;= time_alive), by = Day] 一步完成,无需中间对象。
  • 这完美地回答了我的问题,我的表格输出比循环快 50 倍。我没有考虑过创建一个序列,也没有考虑过“on”功能。谢谢小伙伴们。
  • 太棒了!请接受您认为最有用的答案!
【解决方案2】:

我会以不同的方式解决问题。

如果您运行 data.frame(Alive = cumsum(rev(table(c(1,5,7,5)))))(或者在您的一般情况下为 data.frame(Alive = cumsum(rev(table(DT$time_alive))))),您将获得所需的信息,唯一需要注意的是,如果有任何一天没有死亡,您最终会在数据。

【讨论】:

    【解决方案3】:

    数据表

    library(data.table)
    DT[, .(Day = seq_len(max(time_alive) + 1))
      ][, Number := rowSums(outer(Day, DT$time_alive, `<=`))]
    #      Day Number
    #    <int>  <num>
    # 1:     1      4
    # 2:     2      3
    # 3:     3      3
    # 4:     4      3
    # 5:     5      3
    # 6:     6      1
    # 7:     7      1
    # 8:     8      0
    

    (我假设DT 每个Individual 的行数不超过 1 行。)


    数据

    DT <- setDT(structure(list(Individual = c("ID1", "ID2", "ID3", "ID4"), time_alive = c(1L, 5L, 7L, 5L)), class = c("data.table", "data.frame"), row.names = c(NA, -4L)))
    

    【讨论】:

      猜你喜欢
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      相关资源
      最近更新 更多