根据 R 中其他列中的条件数据更改一年中的第几周值
Change week of year value based on conditional data in other column in R
我有以下 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 intervention:
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)
})
我们可以使用 ifelse
或 case_when
或 replace
而不是 if/else
因为 if/else
未矢量化并且需要单个输入值并输出一个单个布尔值
library(dplyr)
DF %>%
group_by(First.Name) %>%
mutate(WeekofYear = replace(WeekofYear, Intervention == 'PRE', min(WeekofYear)))
#or with case_when
# mutate(WeekofYear = case_when(Intervention == "PRE"~ min(WeekofYear), TRUE ~ WeekofYear))
我有以下 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 intervention:
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)
})
我们可以使用 ifelse
或 case_when
或 replace
而不是 if/else
因为 if/else
未矢量化并且需要单个输入值并输出一个单个布尔值
library(dplyr)
DF %>%
group_by(First.Name) %>%
mutate(WeekofYear = replace(WeekofYear, Intervention == 'PRE', min(WeekofYear)))
#or with case_when
# mutate(WeekofYear = case_when(Intervention == "PRE"~ min(WeekofYear), TRUE ~ WeekofYear))