有条件和分组的mutate dplyr [英] Conditional and grouped mutate dplyr
本文介绍了有条件和分组的mutate dplyr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说一下每个抽屉的袜子增加量的以下数据
Lets say I have the following data of sock increase per drawer
>socks
year drawer_nbr sock_total
1990 1 2
1991 1 2
1990 2 3
1991 2 4
1990 3 2
1991 3 1
我想有一个二进制变量,该变量标识袜子在每个抽屉中是否增加了.如果增加,则为1;否则,则为0.结果将是
I would like to have a binary variable that identifies if the socks have increased in each drawer. 1 if they increased and 0 if not. The result would be
>socks
drawer_nbr growth
<dbl> <factor>
1 0
2 1
3 0
在比较一年的 sock_total
与另一年的 sock_total
时,我陷入了困境.我知道我需要使用 dplyr :: summaries()
,但是我对该函数内部的内容有困难.
I am getting stuck on comparing sock_total
of one year vs sock_total
of another year. I know that I need to use dplyr::summaries()
, but I am having difficulty with what goes inside that function.
推荐答案
如果将1991年与1990年进行比较,则可以执行以下操作:
If you are comparing year 1991 with 1990, you can do:
socks %>%
group_by(drawer_nbr) %>%
summarise(growth = +(sock_total[year == 1991] - sock_total[year == 1990] > 0))
# A tibble: 3 x 2
# drawer_nbr growth
# <int> <int>
#1 1 0
#2 2 1
#3 3 0
这篇关于有条件和分组的mutate dplyr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文