2

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.

2
  • If you have 2+ objects of the same length to loop along, use Map or mapply - df[] <- Map(function(d,f) f(d), df, vct_functions) for example. Commented Dec 15, 2015 at 1:12
  • Thanks. I think the Map() solution is passing two sets of of arguments to a function. I want to apply a vector of n functions to n lists. Commented Dec 15, 2015 at 1:36

2 Answers 2

3

@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])))
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe this..?

lapply(1:ncol(df), FUN=function(i){vct_functionsB[i](df[,i])})

And then unlist/parse the results..

3 Comments

Thanks for the solution. I thought it would work but when I tried it....I received the following message: "error in FUN(X[[i]], ...) : attempt to apply non-function" mmmm??
Pretty sure it's because vct_functionsB[i] should be vct_functionsB[[i]]. Check list(function(x) x)[1](5) against list(function(x) x)[[1]](5)
The single brackets [] return a list while the double brackets return a single element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.