【发布时间】:2019-11-19 18:53:53
【问题描述】:
我有以下 DF(这是一个子集):
structure(list(First.Name = c(6003L, 6003L, 6003L, 6003L, 6003L,
6004L, 6004L, 6004L, 6004L, 6001L, 6001L, 6001L, 6001L, 6002L,
6002L, 6002L, 6002L, 6002L, 6003L, 6003L, 6003L, 6003L, 6004L,
6004L, 6004L), Intervention = c("PRE", "PRE", "PRE", "PRE", "PRE",
"PRE", "PRE", "PRE", "PRE", NA, NA, NA, NA, "PRE", "PRE", "PRE",
"PRE", "PRE", "PRE", "PRE", "PRE", "PRE", "PRE", "PRE", "PRE"
), WeekofYear = c(7, 7, 7, 7, 8, 7, 7, 7, 7, 7, 7, 7, 8, 7, 7,
7, 7, 8, 8, 8, 8, 8, 8, 8, 8)), row.names = c(NA, -25L), groups = structure(list(
First.Name = 6001:6004, .rows = list(10:13, 14:18, c(1L,
2L, 3L, 4L, 5L, 19L, 20L, 21L, 22L), c(6L, 7L, 8L, 9L, 23L,
24L, 25L))), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
看起来像:
# A tibble: 25 x 3
# Groups: First.Name [4]
First.Name Intervention WeekofYear
<int> <chr> <dbl>
1 6003 PRE 7
2 6003 PRE 7
3 6003 PRE 7
4 6003 PRE 7
5 6003 PRE 8
6 6004 PRE 7
7 6004 PRE 7
8 6004 PRE 7
9 6004 PRE 7
10 6001 NA 7
# ... with 15 more rows
我的数据跨越几个星期,我想先按名称汇总数据,然后按一年中的星期。 但是,我想根据干预列重置一些周值。
例如,ID 6003 的第 7 周和第 8 周都标记为 PRE 干预:
First.Name Intervention WeekofYear
<int> <chr> <dbl>
4 6003 PRE 7
5 6003 PRE 8
我想将第 8 周设置为第 7 周,而在这种情况下标签为“PRE”,或者将其他情况设置为该参与者数据的第一周标记为“PRE”的任何内容(请记住,有些标签是 NA) .
所以示例输出:
# A tibble: 25 x 3
# Groups: First.Name [4]
First.Name Intervention WeekofYear
<int> <chr> <dbl>
1 6003 PRE 7
2 6003 PRE 7
3 6003 PRE 7
4 6003 PRE 7
5 6003 PRE 7
6 6004 PRE 7
7 6004 PRE 7
8 6004 PRE 7
9 6004 PRE 7
10 6001 NA 7
# ... with 15 more rows
我尝试了以下各种形式,但没有运气:
FinalDF %>%
group_by(First.Name) %>%
mutate(
if(FinalDF$Intervention == "PRE") {
WeekofYear = min(FinalDF$WeekofYear, na.rm=T)
})
【问题讨论】: