[英]if else statement based on multiple conditions
I have the following table which represents a child, his siblings and the case they are assigned under.我有下表代表一个孩子,他的兄弟姐妹以及他们被分配的情况。 The resource ids represent the house where they were placed together.
资源 ID 代表它们被放置在一起的房子。
child_id|sibling_id|case_id|resource_id
1 8 123 12856
1 9 123 12856
3 11 321 12555
4 12 323 10987
4 13 323 10956
6 14 156 10554
6 15 156 10554
10 16 156 10553
10 17 145 18986
10 18 145 18986
I want to create a new column placed_together
which shows a yes
or a no
for those children that were placed together based on their case_id
s.我想创建一个新列
placed_together
,它显示那些根据case_id
放在一起的孩子的yes
或no
。 So my result should look like this所以我的结果应该是这样的
child_id|sibling_id|case_id|resource_id|placed_together
1 8 123 12856 Yes
1 9 123 12856 Yes
3 11 321 12555 No
4 12 323 10987 No
4 13 323 10956 No
6 14 156 10554 No
6 15 156 10554 No
10 16 156 10553 No
10 17 145 18986 Yes
10 18 145 18986 Yes
Any help would be appreciated.任何帮助,将不胜感激。 I dont know how to create an if statement based on these conditions since a case_id can be the same for a group but their resource id can be different for one of the child.
我不知道如何根据这些条件创建 if 语句,因为 case_id 对于一个组可以是相同的,但是对于其中一个孩子,它们的资源 id 可以不同。
Probably using tidyverse
:可能使用
tidyverse
:
library(tidyverse)
df %>%
group_by(case_id) %>%
mutate(placedTogether = if_else(n()>1 &length(unique(child_id))==1 &
length(unique(resource_id))==1, "Yes", "No"))
# A tibble: 10 x 5
# Groups: case_id [5]
child_id sibling_id case_id resource_id placedTogether
<int> <int> <int> <int> <chr>
1 1 8 123 12856 Yes
2 1 9 123 12856 Yes
3 3 11 321 12555 No
4 4 12 323 10987 No
5 4 13 323 10956 No
6 6 14 156 10554 No
7 6 15 156 10554 No
8 10 16 156 10553 No
9 10 17 145 18986 Yes
10 10 18 145 18986 Yes
Assuming that your dataframe was named df , you can do something like this:假设您的数据框名为df ,您可以执行以下操作:
# create a function that defines if a child is placed together
IsPlacedTogether = function(x, y) ifelse(sum(x == y) > 1, 'Yes', 'No')
# apply this function to every child in your data
df$placed_together = sapply(df$case_id, IsPlacedTogether, df$case_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.