Title: applying list of functions columns in data frame
Tags: r;apply
Question: I have 2 variables, a and b. a is potentially very large. b is always a vector of functions that can be applied to each column in the data frame a.
a <- data.frame(col1=c(1, 2, 3), col2=c(4, 5, 6))
b <- c(as.double, function(x) {1+x})
So the result that I want is that function b[1] is applied to col1, b[2] is applied to col2, and so on for all columns. I feel lapply should be used here but documentation seems to say that it can only have one function. I could use a loop I suppose but a "vectorised" way would be nice.
Comment: Why do you need a loop?

Comment: RScriv, I don't *need* a loop per se, I meant it could be done that way (for each column, get the corresponding indexed function in b and apply) - but as I said would much prefer a vectorized approach

Comment: I guess `mapply` could be of use. See something like `mapply(function(x, y) y(x), x = a, y = b)` perhaps

Here is the accepted answer: This is one way to do it:
results <- mapply(function(i,j) b[[i]](a[[j]]), i=1:length(b), j=1:length(a))
It gives you:
&gt; results
[,1] [,2]
[1,]    1    5
[2,]    2    6
[3,]    3    7


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/20/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r><apply>', 'Title': 'applying list of functions columns in data frame', 'ViewCount': 138, 'id': 22535271, 'user_AboutMe': '', 'user_AccountId': 2780361.0, 'user_DisplayName': 'Manish Patel', 'user_Reputation': 4271.0, 'user_Views': 588.0, 'user_id': 2393012.0}}