R分组多列并为它们指定行
R grouping multiple columns and designating rows for them
对于“nycflights13”包中的“飞机”数据,我有以下任务:
对于每个发动机及其制造商,确定生产年份:最早(最小), 中等和最新(最多)。
我一直在尝试使用 table() 或 tapply() 来解决这个问题,但找不到解决方案。有什么建议吗??
library(nycflights13)
data(planes)
head(planes)
tailnum year type manufacturer model engines seats speed engine
<chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
2 N102UW 1998 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
3 N103US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
4 N104UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
5 N10575 2002 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
6 N105UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
medium 是指获得中位数吗?你可以试试这个 -
library(dplyr)
planes %>%
group_by(engines, manufacturer) %>%
summarise(min = min(year, na.rm = TRUE),
medium = median(year, na.rm = TRUE),
max = max(year, na.rm = TRUE))
在基础 R 中使用 aggregate
-
aggregate(year~engines + manufacturer, planes, function(x)
c(min = min(x, na.rm = TRUE), medium = median(x, na.rm = TRUE),
max = max(x, na.rm = TRUE)))