[英]Creating a new categorical variable based on groups and current categorical variable
I am trying to create a categorical variable based of of groups and current variables.我正在尝试基于组和当前变量创建分类变量。
My current df has the variables: ID, GroupID, and Drinker.我当前的 df 有变量:ID、GroupID 和 Drinker。 I am trying to create a new variable(GroupDrink) to where if any individual(ID) in a group(GroupID) selects yes for Drinker, then all individuals in that group will have a yes for the new variable(GroupDrink).我正在尝试创建一个新变量(GroupDrink),如果组(GroupID)中的任何个人(ID)为饮酒者选择“是”,那么该组中的所有个人都将对新变量(GroupDrink)选择“是”。 Please see the table below for more details.请参阅下表了解更多详情。
ID ID | GroupID组ID | Drinker饮酒者 | GroupDrink(NewVariable) GroupDrink(新变量) |
---|---|---|---|
1 1 | 25 25 | Yes是的 | Yes是的 |
2 2 | 25 25 | No不 | Yes是的 |
3 3 | 21 21 | No不 | No不 |
4 4 | 40 40 | Yes是的 | Yes是的 |
5 5 | 40 40 | No不 | Yes是的 |
6 6 | 40 40 | No不 | Yes是的 |
Does this work:这是否有效:
library(dplyr)
df %>% group_by(GroupID) %>% mutate(GroupDrink = if_else(any(Drinker == 'Yes'), 'Yes','No'))
# A tibble: 6 x 4
# Groups: GroupID [3]
ID GroupID Drinker GroupDrink
<dbl> <dbl> <chr> <chr>
1 1 25 Yes Yes
2 2 25 No Yes
3 3 21 No No
4 4 40 Yes Yes
5 5 40 No Yes
6 6 40 No Yes
Data used:使用的数据:
df
# A tibble: 6 x 3
ID GroupID Drinker
<dbl> <dbl> <chr>
1 1 25 Yes
2 2 25 No
3 3 21 No
4 4 40 Yes
5 5 40 No
6 6 40 No
detach(package:plyr)
library(dplyr)
df %>% group_by(GroupID)%>%
mutate(GroupDrink = case_when
any(Drinker == 'Yes') ~ "Yes",
TRUE~ "No"
Applying functions to groups seems to work better with the case_when function.使用 case_when function 将函数应用于组似乎效果更好。 For this to work, the plyr package has to be uninstalled.为此,必须卸载 plyr package。 I had to detach the package for the function to apply correctly to groups.我必须为 function 分离 package 才能正确应用于组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.