通过 R 中的不同函数聚合不同的行
aggregate different rows by different functions in R
我有以下数据框:
(用于下面测试的 dput())
structure(list(V1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "797 Fleet", class = "factor"),
V2 = structure(c(5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L,
5L, 1L, 4L, 3L, 2L, 5L), .Label = c("Available Hours", "Cycle Time",
"Performance", "Production time", "Units"), class = "factor"),
V3 = c(51, 2989.601111, 2498.85, 540.8754973, 39.93337086,
52, 30010.73389, 24946.62833, 529.4659407, 40.81742793, 36,
20778.5525, 17174.18722, 535.7960907, 40.36234152, 19)), .Names = c("V1",
"V2", "V3"), class = "data.frame", row.names = c(NA, -16L))
我需要聚合数据,但不同的功能有不同的功能;
对于 797 机队,应添加单位和生产时间,但应平均性能和周期时间。
我刚刚尝试使用两个函数进行聚合,但我得到了两列,一列全部添加,另一列全部平均,我只需要一列。
我怎样才能做到这一点?
我不认为有一种直接的方法可以用聚合来做到这一点...
您首先需要使用您感兴趣的特征创建单独的数据集,然后使用所需的函数进行聚合:
t1<-rbind(subset(test, test$V2=="Units"), subset(test, test$V2=="Production time"))
aggregate(.~V2, data=t1, sum)
这是一个解决方案,使用 split() 将数据帧拆分为数据帧列表,每个 V2 级别一个列表项(数据帧),然后分离 lapply 函数以创建具有所需的摘要聚合函数。最后使用 Reduce 和 rbind
将所有组合在一起
df <- structure(list(V1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "797 Fleet", class = "factor"),
V2 = structure(c(5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L,
5L, 1L, 4L, 3L, 2L, 5L), .Label = c("Available Hours", "Cycle Time",
"Performance", "Production time", "Units"), class = "factor"),
V3 = c(51, 2989.601111, 2498.85, 540.8754973, 39.93337086,
52, 30010.73389, 24946.62833, 529.4659407, 40.81742793, 36,
20778.5525, 17174.18722, 535.7960907, 40.36234152, 19)), .Names = c("V1",
"V2", "V3"), class = "data.frame", row.names = c(NA, -16L))
df_list <- split(df, df$V2)
summary <- c(
lapply(df_list[c("Units", "Production time")],
function(df) {aggregate(V3 ~ V1 + V2, data = df, sum)})
,
lapply(df_list[c("Performance", "Cycle Time")],
function(df) {aggregate(V3 ~ V1 + V2, data = df, mean)})
)
Reduce(rbind, summary)
#> V1 V2 V3
#> 1 797 Fleet Units 158.00000
#> 2 797 Fleet Production time 44619.66555
#> 3 797 Fleet Performance 535.37918
#> 4 797 Fleet Cycle Time 40.37105
这是一个使用 data.table
的想法:
library(data.table)
fun_list <- list("Units" = sum, "Production time" = sum, "Performance" = mean, "Cycle Time" = mean)
setDT(df)[V2 %in% names(fun_list), .(res = fun_list[[as.character(.BY[[2]])]](V3)),by = .(V1, V2)]
# V1 V2 res
#1: 797 Fleet Units 158.00000
#2: 797 Fleet Production time 44619.66555
#3: 797 Fleet Performance 535.37918
#4: 797 Fleet Cycle Time 40.37105
让我们稍微解压这个解决方案。首先,我们存储要应用于 V2
中每个值的函数映射。这个列表只是一个函数列表。例如。 "Units" = sum
表示我们要将 sum
应用于 "Units"
组。要查看这是如何工作的,请尝试:fun_list[["Units"]](c(1,2,3))
.
然后我们在 data.table
中通过操作在我们的组中使用它。我们使用存储在 .BY
中的 V2
值来索引我们的函数列表。也就是说,对于每个 V2
值,我们从列表中选择一个函数来应用。这是通过 fun_list[[as.character(.BY[[2]])]]
完成的(注意我们需要 as.character
因为 .BY
是一个因素)。最后,我们将该函数应用于 V3
,这就是 (V3)
在代码 fun_list[[as.character(.BY[[2]])]](V3))
!
的最后部分所做的事情
我有以下数据框:
(用于下面测试的 dput())
structure(list(V1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "797 Fleet", class = "factor"), V2 = structure(c(5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L, 5L), .Label = c("Available Hours", "Cycle Time", "Performance", "Production time", "Units"), class = "factor"), V3 = c(51, 2989.601111, 2498.85, 540.8754973, 39.93337086, 52, 30010.73389, 24946.62833, 529.4659407, 40.81742793, 36, 20778.5525, 17174.18722, 535.7960907, 40.36234152, 19)), .Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA, -16L))
我需要聚合数据,但不同的功能有不同的功能; 对于 797 机队,应添加单位和生产时间,但应平均性能和周期时间。
我刚刚尝试使用两个函数进行聚合,但我得到了两列,一列全部添加,另一列全部平均,我只需要一列。
我怎样才能做到这一点?
我不认为有一种直接的方法可以用聚合来做到这一点... 您首先需要使用您感兴趣的特征创建单独的数据集,然后使用所需的函数进行聚合:
t1<-rbind(subset(test, test$V2=="Units"), subset(test, test$V2=="Production time"))
aggregate(.~V2, data=t1, sum)
这是一个解决方案,使用 split() 将数据帧拆分为数据帧列表,每个 V2 级别一个列表项(数据帧),然后分离 lapply 函数以创建具有所需的摘要聚合函数。最后使用 Reduce 和 rbind
将所有组合在一起df <- structure(list(V1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "797 Fleet", class = "factor"),
V2 = structure(c(5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L,
5L, 1L, 4L, 3L, 2L, 5L), .Label = c("Available Hours", "Cycle Time",
"Performance", "Production time", "Units"), class = "factor"),
V3 = c(51, 2989.601111, 2498.85, 540.8754973, 39.93337086,
52, 30010.73389, 24946.62833, 529.4659407, 40.81742793, 36,
20778.5525, 17174.18722, 535.7960907, 40.36234152, 19)), .Names = c("V1",
"V2", "V3"), class = "data.frame", row.names = c(NA, -16L))
df_list <- split(df, df$V2)
summary <- c(
lapply(df_list[c("Units", "Production time")],
function(df) {aggregate(V3 ~ V1 + V2, data = df, sum)})
,
lapply(df_list[c("Performance", "Cycle Time")],
function(df) {aggregate(V3 ~ V1 + V2, data = df, mean)})
)
Reduce(rbind, summary)
#> V1 V2 V3
#> 1 797 Fleet Units 158.00000
#> 2 797 Fleet Production time 44619.66555
#> 3 797 Fleet Performance 535.37918
#> 4 797 Fleet Cycle Time 40.37105
这是一个使用 data.table
的想法:
library(data.table)
fun_list <- list("Units" = sum, "Production time" = sum, "Performance" = mean, "Cycle Time" = mean)
setDT(df)[V2 %in% names(fun_list), .(res = fun_list[[as.character(.BY[[2]])]](V3)),by = .(V1, V2)]
# V1 V2 res
#1: 797 Fleet Units 158.00000
#2: 797 Fleet Production time 44619.66555
#3: 797 Fleet Performance 535.37918
#4: 797 Fleet Cycle Time 40.37105
让我们稍微解压这个解决方案。首先,我们存储要应用于 V2
中每个值的函数映射。这个列表只是一个函数列表。例如。 "Units" = sum
表示我们要将 sum
应用于 "Units"
组。要查看这是如何工作的,请尝试:fun_list[["Units"]](c(1,2,3))
.
然后我们在 data.table
中通过操作在我们的组中使用它。我们使用存储在 .BY
中的 V2
值来索引我们的函数列表。也就是说,对于每个 V2
值,我们从列表中选择一个函数来应用。这是通过 fun_list[[as.character(.BY[[2]])]]
完成的(注意我们需要 as.character
因为 .BY
是一个因素)。最后,我们将该函数应用于 V3
,这就是 (V3)
在代码 fun_list[[as.character(.BY[[2]])]](V3))
!