检查特定组中是否存在字符,并在R中创建新列
提前感谢您花时间阅读本文。我有一个data.frame,看起来像这样检查特定组中是否存在字符,并在R中创建新列,r,dplyr,match,tidyverse,R,Dplyr,Match,Tidyverse,提前感谢您花时间阅读本文。我有一个data.frame,看起来像这样 time offspring 1 1 1 2 2 1 2 5 3 1 3 4 我想检查每个时间点的子代是否与最后一个时间点的子代匹配。更明确地说,我想看看时间点1和时间点2的后代是否存在于时间点3中 在这种情况下,我希望在新列中为子代赋值1,而不是0.4。 比如说 time offspring alpha
time offspring
1 1
1 2
2 1
2 5
3 1
3 4
我想检查每个时间点的子代是否与最后一个时间点的子代匹配。更明确地说,我想看看时间点1和时间点2的后代是否存在于时间点3中
在这种情况下,我希望在新列中为子代赋值1,而不是0.4。
比如说
time offspring alpha
1 1 1
1 2 0.4
2 1 1
2 5 0.4
3 1 1
3 4 1
非常感谢您的帮助和评论。一个
dplyr
选项可以是:
df %>%
group_by(offspring) %>%
mutate(alpha = pmax(0.4, all(1:3 %in% time)))
time offspring alpha
<int> <int> <dbl>
1 1 1 1
2 1 2 0.4
3 2 1 1
4 2 5 0.4
5 3 1 1
6 3 4 0.4
df%>%
组别(后代)%>%
突变(α=pmax(0.4,全部(1:3%时间)))
时间子代α
1 1 1 1
2 1 2 0.4
3 2 1 1
4 2 5 0.4
5 3 1 1
6 3 4 0.4
如果仅在第三个时间段出现的病例也应视为:
df %>%
group_by(offspring) %>%
mutate(alpha = pmax(0.4, all(1:3 %in% time) | unique(time) == 3))
time offspring alpha
<int> <int> <dbl>
1 1 1 1
2 1 2 0.4
3 2 1 1
4 2 5 0.4
5 3 1 1
6 3 4 1
df%>%
组别(后代)%>%
突变(α=pmax(0.4,全部(1:3%时间)|唯一(时间)==3))
时间子代α
1 1 1 1
2 1 2 0.4
3 2 1 1
4 2 5 0.4
5 3 1 1
6 3 4 1
这两种解决方案都非常简洁。非常感谢@tmfmnk。我很想知道关于代码“mutate(alpha=pmax(0.4,all(1:3%时间)| unique(时间)==3))的每个部分的更多细节”。我知道我要求的太多了,请不要觉得有义务这么做。它检查是否所有值1、2和3都及时出现,或者是否每个组中只有一个唯一的时间值等于3。如果条件满足,则返回TRUE,否则返回FALSE。然后,pmax()。如果条件为真,则最大值为1,如果为假,则最大值为0.4.5。非常聪明的思维