我有一堆函数是ddply和其他plyr函数的包装器。我偶尔需要更改我在这些函数中使用的分组变量,并且我想将它们保存在全局变量中,所以我只需要改变一件事来影响所有函数的行为。这就是我正在尝试的。
# Grouping variables to pass to ddply
params = c('density', 'decay_rate', 'scale', 'exponent', 'max_distance')
location = c('grid_x', 'grid_y', 'dataset_x', 'dataset_y')
mean_d <- function(df) {
# mean function to call from ddply
mean_likelihood <- function (x) {
mean_likelihood <- mean(x$likelihood)
return(mean_likelihood)
}
# This doesn't work.
# mean_df <- ddply(df, .(seed, params, location), mean_likelihood)
# This works
mean_df <- ddply(df, .(seed,
density, decay_rate, scale, exponent,
max_distance, grid_x, grid_y, dataset_x, dataset_y),
mean_likelihood)
names(mean_df)[length(names(mean_df))] <- 'mean_likelihood'
return(mean_df)
}
答案 0 :(得分:1)
library(ggplot2)
data(diamonds)
small_diamonds <- diamonds[sample(nrow(diamonds), 100), ]
set1 <- c("cut", "color")
set2 <- c("cut", "color", "clarity")
foo <- function(df) {
return(df[which.max(df$depth), ])
}
ddply(small_diamonds, set1, foo)
ddply(small_diamonds, set2, foo)