Reputation: 1085
I am trying to develop a reusable solution to the following problem.
I will have various data frames and I wish to define some basic functions and then apply these functions to the various columns in the data frame. The result would be a data frame that has the same number of rows and columns as the original data frame.
I want this solution to be reusable insofar that different combinations of functions can be applied to different data frames.
Its a bit tricky to explain this in words. I am hoping the code below will make the above a bit clearer.
# construct an example data frame
v_a <- c(1, 4, 8, 10, 12)
v_b <- c(4, 6, 7, 4, 3)
v_c <- c(10, 23, 45, 12, 45)
v_d <- c(12, 45, 7, 10, 5)
df <- data.frame(a = v_a, b = v_b, c = v_c, d = v_d)
# define some example functions
fn_1 <- function(x) x * 3
fn_2 <- function(x) x * 4
# assemble functions into a vector
vct_functions <- c(fn_1, fn_2, fn_1, fn_1)
# apply vector of functions to columns in a data.frame
new_df <- fn_apply_functionList(df, vct_functions)
I am hoping to reuse the solution above such that a different combination of functions could be applied to the same or a different data frame.
Again, the following is a code snippet to make this clearer.
# create another vector of functions
vct_functionsB <- c(fn_3, fn_4, fn_1, fn_2)
# apply vector of functions to columns in a data.frame
new_df <- fn_apply_functionList(another_df, vct_functions)
Any ideas on an elegant solution would be really appreciated.
Upvotes: 2
Views: 608
Reputation: 1057
@geotheory was almost there, vct_functionsB
is coerced to a list
Try this,
do.call(cbind, lapply(1:ncol(df), function(c) vct_functions[[c]](df[, c])))
Upvotes: 3
Reputation: 23680
Maybe this..?
lapply(1:ncol(df), FUN=function(i){vct_functionsB[i](df[,i])})
And then unlist/parse the results..
Upvotes: 0