我得到了一些数据,比如这个
structure(list(id = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3), adm = c("1",
"2", "3", "Dead", "1", "2", "3", "1", "2", "3", "4", "Dead"),
status = c("dead", "dead", "dead", "dead", "alive", "alive",
"alive", "dead", "dead", "dead", "dead", "dead")), class = "data.frame", row.names = c(NA, -12L))
我想要这种
structure(list(id2 = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3),
adm2 = c("1", "2", "3", "Dead", "1", "2", "3", "alive", "1",
"2", "3", "4", "Dead"), status2 = c("dead", "dead", "dead",
"dead", "alive", "alive", "alive", "alive", "dead", "dead",
"dead", "dead", "dead")), class = "data.frame", row.names = c(NA, -13L))
我想考虑id
列,如果在每个id的列(adm
(中的最大条目之后,状态列是活动的,那么在adm
列中的最大值之后,将状态列中的值添加到adm列中。
将起始数据命名为df
,可以创建一个用户定义的函数来评估行插入条件,并将其映射到按id
:分组的数据
library(tidyverse)
appendcol <- function(df) {
if (df$status[length(df$status)] == "alive") {
newrow <- df[1,]
newrow$adm <- "alive"
return(bind_rows(df, newrow))
} else {
return(df)
}
}
map_dfr(group_split(df, id), ~ appendcol(.))
输出:
# A tibble: 13 x 3
id adm status
<dbl> <chr> <chr>
1 1 1 dead
2 1 2 dead
3 1 3 dead
4 1 Dead dead
5 2 1 alive
6 2 2 alive
7 2 3 alive
8 2 alive alive
9 3 1 dead
10 3 2 dead
11 3 3 dead
12 3 4 dead
13 3 Dead dead