更新时间:2022-11-14 14:11:02
让 Dickoa 的答案更进一步——正如 Hadley 所说的总结剥离了单层分组".它从您应用它的相反顺序剥离分组,以便您可以使用
Taking Dickoa's answer one step further -- as Hadley says "summarise peels off a single layer of grouping". It peels off grouping from the reverse order in which you applied it so you can just use
mtcars %>%
group_by(cyl, gear) %>%
summarise(newvar = sum(wt)) %>%
summarise(newvar2 = sum(newvar) + 5)
请注意,如果您在第二行中使用 group_by(gear, cyl)
,这将给出不同的答案.
Note that this will give a different answer if you use group_by(gear, cyl)
in the second line.
为了让您的第一次尝试成功:
And to get your first attempt working:
df1 <- mtcars %>%
group_by(cyl, gear) %>%
summarise(newvar = sum(wt))
df2 <- df1 %>%
group_by(cyl) %>%
summarise(newvar2 = sum(newvar)+5)