R:如何将函数应用于某些列中的数据组?

时间:2018-10-07 20:50:08

标签: r function

感谢您为将功能应用于按某些列分组的数据框所提供的帮助。我想我必须使用某些dplyr函数或lapplydo.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)使用预先定义的hypervolumematrix_point计算refp
  • refp是用于每次计算的向量(RP <- c(1.0,1.0))。

问题取决于matrix_points:

  • matrix_points是一个与我的数据帧相比转置的矩阵。
  • 我需要使用由 Cen Mod 分组的所有Indiv的 Obj1 Obj2 计算出的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

感谢您的帮助。

2 个答案:

答案 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.callordermerge变成bind_rowsarrange,{{1 }}:

inner_join