【问题标题】:Duplicate rows in data frame based on Date基于日期的数据框中的重复行
【发布时间】:2017-02-04 15:07:25
【问题描述】:

为以下问题寻找一个简单的解决方案 这就是我的数据的样子:

ClientID    PatientID   Measure Value   CollectionDatetime
41  123456  Temperature           87    02-04-2017
41  123456  WBC                 1000    02-04-2017
41  123456  Temperature           83    02-05-2017
41  23456   WBC                10000    02-04-2017
41  23456   RR                   100    02-04-2017
41  23456   C-Ceratine            90    02-05-2017
41  23456   Temperature           87    02-06-2017
41  23456   Temperature           89    02-06-2017

这就是我想要的输出:

ClientID     PatientID  Measure Value   CollectionDatetime  Label
41  123456  Temperature            87   02-04-2017            1
41  123456  WBC                  1000   02-04-2017            1
41  123456  Temperature            87   02-04-2017            2
41  123456  WBC                  1000   02-04-2017            2
41  123456  Temperature            83   02-05-2017            2
41  23456   WBC                 10000   02-04-2017            1
41  23456   RR                    100   02-04-2017            1
41  23456   WBC                 10000   02-04-2017            2
41  23456   RR                    100   02-04-2017            2
41  23456   C-Ceratine             90   02-05-2017            2
41  23456   WBC                 10000   02-04-2017            3
41  23456   RR                    100   02-04-2017            3
41  23456   C-Ceratine             90   02-05-2017            3
41  23456   Temperature            87   02-06-2017            3
41  23456   Temperature            89   02-06-2017            3

应根据患者 ID 和 CollectionDatetime 复制数据。 对于每个 Patient ID,如果是第 1 天一次,第 2 天应该有第 1 天和第 2 天的数据,以此类推

【问题讨论】:

  • 你是如何得到标签的?
  • 似乎不清楚。关于重复值和构造“标签”的规则需要扩展。
  • 看起来像一个扩展窗口;每个日期应包含当前日期和所有先前日期的行,按 ClientID 分组。这个例子当然可以更简洁。

标签: r function dataframe duplicates apply


【解决方案1】:

使用data.table-package:

# load the data.table package & convert 'dat' to a data.table
library(data.table)
setDT(dat)

# create the 'lbl' variable and the number of times each row needs to be repeated

dat[, lbl := rleid(CollectionDatetime), PatientID
    ][, reps := abs(lbl - max(lbl)), PatientID]

# create a 2nd data.table with the repeated rows
# make a sequence for each replication
# add that to 'lbl' to get correct 'lbl'

d2 <- dat[rep(1:nrow(dat), reps)][, lbl := lbl + 1:max(reps), .(PatientID,lbl)]

# bind the original data.table and the new together
# remove 'reps' column (no longer needed)
# and order to match the expected output

rbindlist(list(dat,d2))[, reps := NULL][order(-PatientID,lbl,CollectionDatetime)]

给予:

    ClientID PatientID     Measure Value CollectionDatetime lbl
 1:       41    123456 Temperature    87         2017-02-04   1
 2:       41    123456         WBC  1000         2017-02-04   1
 3:       41    123456 Temperature    87         2017-02-04   2
 4:       41    123456         WBC  1000         2017-02-04   2
 5:       41    123456 Temperature    83         2017-02-05   2
 6:       41     23456         WBC 10000         2017-02-04   1
 7:       41     23456          RR   100         2017-02-04   1
 8:       41     23456         WBC 10000         2017-02-04   2
 9:       41     23456          RR   100         2017-02-04   2
10:       41     23456  C-Ceratine    90         2017-02-05   2
11:       41     23456         WBC 10000         2017-02-04   3
12:       41     23456          RR   100         2017-02-04   3
13:       41     23456  C-Ceratine    90         2017-02-05   3
14:       41     23456 Temperature    87         2017-02-06   3
15:       41     23456 Temperature    89         2017-02-06   3

您可以在基础 R 中实现相同的效果:

dat$lbl <- with(dat, ave(as.numeric(CollectionDatetime), PatientID, FUN = function(x) cumsum(c(1, diff(x) > 0))))
dat$reps <- with(dat, ave(lbl, PatientID, FUN = function(x) abs(x - max(x))))

dat2 <- dat[rep(1:nrow(dat), dat$reps),]
dat2$lbl <- dat2$lbl + with(dat2, ave(reps, cumsum(c(0,abs(diff(dat2$reps)))), FUN = function(x) 1:max(x)))

d <- rbind(dat,dat2)[,-7]
d[order(-d$PatientID,d$lbl,d$CollectionDatetime),]

使用过的数据:

dat <- structure(list(ClientID = c(41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L), 
                      PatientID = c(123456L, 123456L, 123456L, 23456L, 23456L, 23456L, 23456L, 23456L), 
                      Measure = structure(c(3L, 4L, 3L, 4L, 2L, 1L, 3L, 3L), .Label = c("C-Ceratine", "RR", "Temperature", "WBC"), class = "factor"), 
                      Value = c(87L, 1000L, 83L, 10000L, 100L, 90L, 87L, 89L), 
                      CollectionDatetime = structure(c(17201, 17201, 17202, 17201, 17201, 17202, 17203, 17203), class = "Date")), 
                 .Names = c("ClientID", "PatientID", "Measure", "Value", "CollectionDatetime"), row.names = c(NA, -8L), class = "data.frame")

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2018-04-01
    相关资源
    最近更新 更多