在dplyr中管道 - 我如何组合数据

时间:2017-09-14 09:54:11

标签: r dplyr

我试图用dplyr来理解管道。示例中包含presidential的{​​{1}}数据集。

ggplot2

所以我计算了每个总统的工作时间。现在我想总结每个政党总统的时间,但我得到他们拥有的总统数量。

library(ggplot2)
library(dplyr)

data("presidential")
presidential %>%
  select(name,start,end,party) %>%
  mutate(time = end - start) %>%
  group_by(party) %>%
  mutate(time_per_party = length(time)) -> x
x

任何想法怎么做? 最后的结果应该是这样的:

         name      start        end      party      time time_per_party
        (chr)     (date)     (date)      (chr)    (dfft)          (int)
1  Eisenhower 1953-01-20 1961-01-20 Republican 2922 days              6
2     Kennedy 1961-01-20 1963-11-22 Democratic 1036 days              4
3      Johson 1963-11-22 1969-01-20 Democratic 1886 days              4
4       Nixon 1969-01-20 1974-08-09 Republican 2027 days              6
5        Ford 1974-08-09 1977-01-20 Republican  895 days              6
6      Carter 1977-01-20 1981-01-20 Democratic 1461 days              4
7      Reagan 1981-01-20 1989-01-20 Republican 2922 days              6
8        Bush 1989-01-20 1993-01-20 Republican 1461 days              6
9     Clinton 1993-01-20 2001-01-20 Democratic 2922 days              4
10       Bush 2001-01-20 2009-01-20 Republican 2922 days              6

2 个答案:

答案 0 :(得分:2)

发现一种与Z.Lin评论非常相似的解决方案:

presidential %>%  mutate(time = end - start) %>% group_by(party) %>%  summarise(days = sum(time))

诀窍

答案 1 :(得分:1)

List<int> items = new List<int>() { 1, 3, 1, 2 };