我想了解如何使用dplyr包编写以下代码:
Book
这是原始数据框:
averageStepsDayType <- aggregate(
NAreplacement$steps,
by=list(interval=NAreplacement$interval, dayType=NAreplacement$dayType),
mean
)
这是使用上面代码获得的所需输出:
> head(NAreplacement)
steps date interval dayType
1 1.7169811 2012-10-01 0 weekday
2 0.3396226 2012-10-01 5 weekday
3 0.1320755 2012-10-01 10 weekday
4 0.1509434 2012-10-01 15 weekday
5 0.0754717 2012-10-01 20 weekday
6 2.0943396 2012-10-01 25 weekday
这是我的尝试:
> head(averageStepsDayType)
interval dayType x
1 0 weekday 2.25115304
2 5 weekday 0.44528302
3 10 weekday 0.17316562
4 15 weekday 0.19790356
5 20 weekday 0.09895178
6 25 weekday 1.59035639
有人可以举个例子吗?我想它应该是select和group_by函数之间的组合,但我还没弄清楚如何。
答案 0 :(得分:2)
尝试
library(dplyr)
NAreplacement %>%
group_by(interval, dayType) %>%
summarise(x=mean(steps))