感谢您为将功能应用于按某些列分组的数据框所提供的帮助。我想我必须使用某些dplyr
函数或lapply
或do.call
,但我无法做到这一点。
我有以下数据框:
dfFull <- data.frame(Cen = c("Cen01", "Cen01", "Cen01", "Cen01",
"Cen01", "Cen01", "Cen01", "Cen01",
"Cen02", "Cen02", "Cen02", "Cen02",
"Cen02", "Cen02", "Cen02", "Cen02"),
Model = c("Mod01", "Mod01", "Mod01", "Mod01",
"Mod02", "Mod02", "Mod02", "Mod02",
"Mod01", "Mod01", "Mod01", "Mod01",
"Mod02", "Mod02", "Mod02", "Mod02"),
Indiv = c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4),
PF = c(1,1,2,2,1,1,2,2,1,1,2,2,1,1,2,2),
Obj1 = c(0.0,0.02,0.01,0.03,0.01,
0.0,0.02,0.0,0.15,0.03,
0.02,0.08,0.1,0.06,0.02,0.09),
Obj2 = c(0.8,0.62,0.85,0.7,0.92,
0.26,0.85,0.93,0.03,0.84,
0.94,0.84,0.05,0.63,0.83,0.92))
我必须调用一个函数(来自 emoa 包):
dominated_hypervolume(matrix_points, refp)
使用预先定义的hypervolume
为matrix_point
计算refp
。 refp
是用于每次计算的向量(RP <- c(1.0,1.0)
)。 问题取决于matrix_points:
matrix_points
是一个与我的数据帧相比转置的矩阵。 hypervolume
em>和 PF 列。 使用小数据,我知道dominated_hypervolume
可以完成工作,因为我能够提供适当的数据。
我知道这是错误的,但我正在尝试执行以下操作:
dfFull <- dfFull %>%
group_by(Cen, Model, PF) %>%
do.call(HV =dominated_hypervolume(data.matrix(t(dfFull[,5:6]), RP)))
最后我期望的是以下内容。 HV
值仅是示例,而不是计算得出的值。对计算中使用的个人行重复HV
值不是问题。
Cen Model PF Indiv Obj1 Obj2 HV
Cen01 Mod01 1 1 0.0 0.8 0.77
Cen01 Mod01 1 2 0.02 0.62 0.77
Cen01 Mod01 2 3 0.01 0.85 0.74
Cen01 Mod01 2 4 0.03 0.70 0.74
Cen01 Mod02 1 1 0.01 0.92 0.81
Cen01 Mod02 1 2 0.0 0.26 0.81
Cen01 Mod02 2 3 0.02 0.85 0.69
Cen01 Mod02 2 4 0.0 0.93 0.69
Cen02 Mod01 1 1 0.15 0.03 0.88
Cen02 Mod01 1 2 0.03 0.84 0.88
Cen02 Mod01 2 3 0.02 0.94 0.86
Cen02 Mod01 2 4 0.08 0.84 0.86
Cen02 Mod02 1 1 0.1 0.05 0.76
Cen02 Mod02 1 2 0.06 0.63 0.76
Cen02 Mod02 2 3 0.02 0.83 0.64
Cen02 Mod02 2 4 0.09 0.92 0.64
感谢您的帮助。
答案 0 :(得分:3)
library(tidyverse)
library(emoa)
RP <- c(1.0,1.0)
dfFull %>%
nest(-Cen, -Model, -PF) %>%
mutate(HV = map_dbl(data, ~dominated_hypervolume(t(data.frame(.x$Obj1, .x$Obj2)), RP))) %>%
unnest()
# Cen Model PF HV Indiv Obj1 Obj2
# 1 Cen01 Mod01 1 0.3764 1 0.00 0.80
# 2 Cen01 Mod01 1 0.3764 2 0.02 0.62
# 3 Cen01 Mod01 2 0.2940 3 0.01 0.85
# 4 Cen01 Mod01 2 0.2940 4 0.03 0.70
# 5 Cen01 Mod02 1 0.7400 1 0.01 0.92
# 6 Cen01 Mod02 1 0.7400 2 0.00 0.26
# 7 Cen01 Mod02 2 0.1484 3 0.02 0.85
# 8 Cen01 Mod02 2 0.1484 4 0.00 0.93
# 9 Cen02 Mod01 1 0.8437 1 0.15 0.03
# 10 Cen02 Mod01 1 0.8437 2 0.03 0.84
# 11 Cen02 Mod01 2 0.1508 3 0.02 0.94
# 12 Cen02 Mod01 2 0.1508 4 0.08 0.84
# 13 Cen02 Mod02 1 0.8698 1 0.10 0.05
# 14 Cen02 Mod02 1 0.8698 2 0.06 0.63
# 15 Cen02 Mod02 2 0.1666 3 0.02 0.83
# 16 Cen02 Mod02 2 0.1666 4 0.09 0.92
答案 1 :(得分:0)
考虑by
(用于分组的tapply
的面向对象包装器)传递分组的子集以运行所需的已定义方法,然后将其转换为data.frame()
:
grpcols <- c("Cen", "Model", "PF")
df_list <- by(dfFull, dfFull[grpcols], function(sub)
data.frame(Cen = sub$Cen[[1]],
Model = sub$Model[[1]],
PF = sub$PF[[1]],
HV = dominated_hypervolume(t(sub[,5:6]), RP))
)
然后处理数据帧列表:
# BASE PROCESSING
final_df <- do.call(rbind, df_list)
final_df <- with(final_df, final_df[order(Cen, Model, PF),])
row.names(final_df) <- NULL
final_df <- merge(dfFull, final_df[c(grpcols, "HV")], by=c("Cen", "Model", "PF"))
final_df
# Cen Model PF Indiv Obj1 Obj2 HV
# 1 Cen01 Mod01 1 1 0.00 0.80 0.3764
# 2 Cen01 Mod01 1 2 0.02 0.62 0.3764
# 3 Cen01 Mod01 2 3 0.01 0.85 0.2940
# 4 Cen01 Mod01 2 4 0.03 0.70 0.2940
# 5 Cen01 Mod02 1 1 0.01 0.92 0.7400
# 6 Cen01 Mod02 1 2 0.00 0.26 0.7400
# 7 Cen01 Mod02 2 3 0.02 0.85 0.1484
# 8 Cen01 Mod02 2 4 0.00 0.93 0.1484
# 9 Cen02 Mod01 1 1 0.15 0.03 0.8437
# 10 Cen02 Mod01 1 2 0.03 0.84 0.8437
# 11 Cen02 Mod01 2 3 0.02 0.94 0.1508
# 12 Cen02 Mod01 2 4 0.08 0.84 0.1508
# 13 Cen02 Mod02 1 1 0.10 0.05 0.8698
# 14 Cen02 Mod02 1 2 0.06 0.63 0.8698
# 15 Cen02 Mod02 2 3 0.02 0.83 0.1666
# 16 Cen02 Mod02 2 4 0.09 0.92 0.1666
或者使用dplyr处理仍使用base::by()
输出,其中do.call
,order
,merge
变成bind_rows
,arrange
,{{1 }}:
inner_join