[R] Applying a function to a column of a data frame

Onur Uncu onuruncu at gmail.com
Sat Jun 9 19:02:04 CEST 2012


Thank you Michael. One follow up question: In the solution using
apply(), why are we feeding x[2] inside testfun instead of x[[2]]?
Aren't we supposed to feed the vector of values to the function, which
is achieved by x[[2]]?

Thnks.


On Sat, Jun 9, 2012 at 3:38 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
> 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.



More information about the R-help mailing list