I have a bunch of functions that are wrappers for ddply and other plyr functions. I occasionally need to change the grouping variables that I use in those functions and I'd like to keep them in a global variable so I only have to change one thing to effect the behavior of all the functions. Here's what I'm trying.
我有一堆函数是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)
}
1 个解决方案
#1
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)
#1
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)