根据另一列指定列值(拆分应用合并)

时间:2017-03-25 18:26:55

标签: dplyr split-apply-combine

我有格式数据

set.seed(40)
subject <- sample(c("mike", "john", "steve"), 20, replace = TRUE)
test1 <- sample(c("pos", "neg", "pos", "neg", "NA"), 20, replace = TRUE)
testdate <- Sys.Date() + sample(-1000:1000, 20, replace = FALSE)
mydf <- data.frame(subject, testdate, test1)
mydf$status <- "unknown"

对于每个受试者,我想修改状态值,使得:a)在获得test1的最早(通过测试日期)pos或neg结果之前,它仍然是未知的; b)当获得第一个pos test1结果时,该日期和之后的状态变为“in”,而不管后续的test1值如何; c)如果在任何阳性结果之前对test1发生了否定结果,那么状态将变为“out”,直到获得任何阳性test1结果为止。对所有解决方案开放。我正在尝试使用dplyr,并且对基于dplyr的解决方案特别感兴趣。

输出为

subject testdate    test1   status
john    2014-11-20  neg negative
john    2015-07-29  neg negative
john    2015-11-10  neg negative
john    2017-04-08  neg negative
john    2018-09-18  NA  negative
mike    2014-09-01  pos positive
mike    2014-10-14  neg positive
mike    2015-03-22  neg positive
mike    2016-09-15  pos positive
mike    2017-08-18  neg positive
mike    2017-12-20  pos positive
mike    2018-09-06  NA  positive
mike    2019-09-02  neg positive
steve   2015-06-21  neg negative
steve   2016-01-03  pos positive
steve   2016-03-12  neg positive
steve   2017-06-26  neg positive
steve   2017-12-02  neg positive
steve   2018-12-20  pos positive
steve   2019-06-20  pos positive

1 个答案:

答案 0 :(得分:0)

group_bymutate相对简单。

首先,将测试结果修改为一个因素。这使他们能够被排名&#34;这样我们就能分辨出最高的&#34;结果一直是。因为您希望结果为&#34; Missing&#34;,&#34; Negative&#34;,&#34; Positive&#34 ;,按此顺序设置级别:

mydf$test1 <-
  factor(mydf$test1
         , levels = c("NA", "neg", "pos")
         , ordered = TRUE)

接下来,创建上述每个标签时要使用的标签的矢量。在文中,您说您想进/出,但所需的输出使用负/正。如果您想更改标签,请在此处轻松完成:

statusLevels <-
  c("Unknown", "Negative", "Positive")

最后,我们可以将其应用于数据。首先,按日期排序以确保以正确的顺序检查测试结果(我还按主题排序以使结果清晰并符合您的请求)。然后,按主题分组。最后,mutate创建您想要的列。在这里,它检查&#34;最大&#34;到目前为止的测试值(因此我们转换为一个因子的原因)并给出了匹配的状态级别:

mydf %>%
  arrange(subject, testdate) %>%
  group_by(subject) %>%
  mutate(status = statusLevels[cummax(as.numeric(test1))])

返回:

   subject   testdate test1   status
     <chr>     <date> <ord>    <chr>
1     john 2014-11-21   neg Negative
2     john 2015-07-30   neg Negative
3     john 2015-11-11   neg Negative
4     john 2017-04-09   neg Negative
5     john 2018-09-19    NA Negative
6     mike 2014-09-02   pos Positive
7     mike 2014-10-15   neg Positive
8     mike 2015-03-23   neg Positive
9     mike 2016-09-16   pos Positive
10    mike 2017-08-19   neg Positive
11    mike 2017-12-21   pos Positive
12    mike 2018-09-07    NA Positive
13    mike 2019-09-03   neg Positive
14   steve 2015-06-22   neg Negative
15   steve 2016-01-04   pos Positive
16   steve 2016-03-13   neg Positive
17   steve 2017-06-27   neg Positive
18   steve 2017-12-03   neg Positive
19   steve 2018-12-21   pos Positive
20   steve 2019-06-21   pos Positive