r有条件地突变为特定组的行
我想突变某些行符合符合另一个条件的特定组的条件。
目的:
例如,我正在尝试从下面的数据集中提取母亲的名字,并将其应用于以下数据集中所有“一夫一妻制”的群体标记的行旁边的行旁边:
df <- tribble(
~family, ~sequence, ~role, ~state, ~name, ~year_of_birth,
"A", 1, "Father", "Monogamist", "Adam", 1980,
"A", 2, "Mother", "Monogamist", "Sarah", 1981,
"A", 3, "Child", "Monogamist", "Omar", 2000,
"A", 4, "Child", "Monogamist", "Joseph", 2001,
"B", 1, "Father", "Polygamist", "Ali", 1990,
"B", 2, "Mother", "Polygamist", "Miriam", 1998,
"B", 2, "Child", "Polygamist", "Noah", 1992,
"B", 3, "Child", "Polygamist", "Jacob", 1998,
"B", 4, "Child", "Polygamist", "Layla", 2014,
"C", 1, "Father", "Widower", "Ibrahim", 2020,
"C", 3, "Child", "Widower", "Zakariya", 2021,
"C", 4, "Child", "Widower", "Kahlid", 2022,
)
问题是:
问题是,一个妻子的发生或没有导致我的尝试/审判失败。
我的尝试:
我的attemp是有条件地突变这样的行链接与以下内容:
# Not-working
df %>%
group_by(family) %>%
mutate(mother_name = case_when(!sequence %in% c(1,2) ~ name[sequence == 2],TRUE ~ ""))
Error in `mutate()`:
! Problem while computing `mother_name = case_when(...)`.
i The error occurred in group 2: family = "B".
Caused by error in `case_when()`:
! `!sequence %in% c(1, 2) ~ name[sequence == 2]` must be length 5 or one, not 2.
Run `rlang::last_error()` to see where the error occurred.
# Working
df %>%
group_by(family) %>%
filter(any(state == "Monogamist")) %>%
mutate(mother_name = case_when(!sequence %in% c(1,2) ~ name[sequence == 2],TRUE ~ ""))
# A tibble: 4 x 7
# Groups: family [1]
family sequence role state name year_of_birth mother_name
<chr> <dbl> <chr> <chr> <chr> <dbl> <chr>
1 A 1 Father Monogamist Adam 1980 ""
2 A 2 Mother Monogamist Sarah 1981 ""
3 A 3 Child Monogamist Omar 2000 "Sarah"
4 A 4 Child Monogamist Joseph 2001 "Sarah"
预期输出
如何实现下面的输出。根据出生年份结合母亲的名字会很好。添加此条件any(state ==“一夫一妻制”)
case_时是使我卡住的原因。
family sequence role state name year_of_birth mother_name
1 A 1 Father Monogamist Adam 1980 NA
2 A 2 Mother Monogamist Sarah 1981 Sarah
3 A 3 Child Monogamist Omar 2000 NA
4 A 4 Child Monogamist Joseph 2001 NA
5 B 1 Father Polygamist Ali 1990 NA
6 B 2 Mother Polygamist Miriam 1998 Fatima, Miriam
7 B 2 Child Polygamist Fatima 1992 Fatima, Miriam
8 B 3 Child Polygamist Jacob 1998 NA
9 B 4 Child Polygamist Layla 2014 NA
10 C 1 Father Widower Ibrahim 2020 NA
11 C 3 Child Widower Zakariya 2021 NA
12 C 4 Child Widower Kahlid 2022 NA
I would like to mutate certain rows meeting a condition for specific groups that meet another condition.
The Aim:
For example, I'm trying to extract the mother's name from the below dataset and apply it beside rows labelled with 'children' for only groups that has all 'Monogamists' in the below dataset:
df <- tribble(
~family, ~sequence, ~role, ~state, ~name, ~year_of_birth,
"A", 1, "Father", "Monogamist", "Adam", 1980,
"A", 2, "Mother", "Monogamist", "Sarah", 1981,
"A", 3, "Child", "Monogamist", "Omar", 2000,
"A", 4, "Child", "Monogamist", "Joseph", 2001,
"B", 1, "Father", "Polygamist", "Ali", 1990,
"B", 2, "Mother", "Polygamist", "Miriam", 1998,
"B", 2, "Child", "Polygamist", "Noah", 1992,
"B", 3, "Child", "Polygamist", "Jacob", 1998,
"B", 4, "Child", "Polygamist", "Layla", 2014,
"C", 1, "Father", "Widower", "Ibrahim", 2020,
"C", 3, "Child", "Widower", "Zakariya", 2021,
"C", 4, "Child", "Widower", "Kahlid", 2022,
)
The problem:
The problem is that more occurance of one wife or have none causes my attempt/trial to fail.
My Attempt:
My attemp is to conditionally mutate such rows link with the below:
# Not-working
df %>%
group_by(family) %>%
mutate(mother_name = case_when(!sequence %in% c(1,2) ~ name[sequence == 2],TRUE ~ ""))
Error in `mutate()`:
! Problem while computing `mother_name = case_when(...)`.
i The error occurred in group 2: family = "B".
Caused by error in `case_when()`:
! `!sequence %in% c(1, 2) ~ name[sequence == 2]` must be length 5 or one, not 2.
Run `rlang::last_error()` to see where the error occurred.
# Working
df %>%
group_by(family) %>%
filter(any(state == "Monogamist")) %>%
mutate(mother_name = case_when(!sequence %in% c(1,2) ~ name[sequence == 2],TRUE ~ ""))
# A tibble: 4 x 7
# Groups: family [1]
family sequence role state name year_of_birth mother_name
<chr> <dbl> <chr> <chr> <chr> <dbl> <chr>
1 A 1 Father Monogamist Adam 1980 ""
2 A 2 Mother Monogamist Sarah 1981 ""
3 A 3 Child Monogamist Omar 2000 "Sarah"
4 A 4 Child Monogamist Joseph 2001 "Sarah"
Expected output
How to achieve the below output. It would be nice to combine mother's name based on year of birth ascendingly. Adding this condition any(state == "Monogamist")
to the case_when
is what making me stuck.
family sequence role state name year_of_birth mother_name
1 A 1 Father Monogamist Adam 1980 NA
2 A 2 Mother Monogamist Sarah 1981 Sarah
3 A 3 Child Monogamist Omar 2000 NA
4 A 4 Child Monogamist Joseph 2001 NA
5 B 1 Father Polygamist Ali 1990 NA
6 B 2 Mother Polygamist Miriam 1998 Fatima, Miriam
7 B 2 Child Polygamist Fatima 1992 Fatima, Miriam
8 B 3 Child Polygamist Jacob 1998 NA
9 B 4 Child Polygamist Layla 2014 NA
10 C 1 Father Widower Ibrahim 2020 NA
11 C 3 Child Widower Zakariya 2021 NA
12 C 4 Child Widower Kahlid 2022 NA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的解决方案:
使用
角色
的解决方案:或使用
sequence
:输出:
new new根据您的说明数据:
The solution below:
A solution using
role
:Or using
sequence
:Output:
New data according to your instructions: