Apologees the novice question. Currently climbing up the learning curve of R. Suppose I have the following function and the data.frame: testfun<-function(x=1,y=2) x+y testframe=data.frame(col1=c(1,2),col2=c(3,4)) When evaluating testfun, I want to use the default value for y (which is 2) and for x, I want to feed (one by one) the values in col2 of testframe. How can I achieve this please? Not having much success with apply() Thanks for any help.
R. Michael Weylandt
2012-Jun-09 14:38 UTC
[R] Applying a function to a column of a data frame
No worries -- it's an important question and it introduces you to one of the most important R idioms. [And you get bonus points for having a well formed question!] You're probably looking for something like this: apply(testframe, 1, function(x) testfun(x[2])) Which goes row-by-row and substitutes the second element (your col2) into testfun leaving the y= unsupplied and hence defaulting to 2. This seems a little kludgy, however, in light of the below. (Also, if you have a data frame of multiple types, it seems to get in trouble quicker than I would have imagined) Note that this is not particularly efficient and you could just use testfun(testframe[["col2"]]) instead using the magic of vectorization. Very rarely do you need to do things "one-by-one" except for recursive/state-dependent simulations. Best, Michael On Sat, Jun 9, 2012 at 9:12 AM, Onur Uncu <onuruncu at gmail.com> wrote:> Apologees the novice question. Currently climbing up the learning curve of R. > > Suppose I have the following function and the data.frame: > > testfun<-function(x=1,y=2) x+y > > testframe=data.frame(col1=c(1,2),col2=c(3,4)) > > When evaluating testfun, I want to use the default value for y (which > is 2) and for x, I want to feed (one by one) the values in col2 of > testframe. How can I achieve this please? Not having much success with > apply() > > Thanks for any help. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.