[R] Applying a function to a column of a data frame
David Winsemius
dwinsemius at comcast.net
Sat Jun 9 19:21:52 CEST 2012
On Jun 9, 2012, at 1:02 PM, Onur Uncu wrote:
> 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]]?
`apply` sends the columns of data to the receiving function as a
vector. It has already effectively done an unlist operation on
dfrm[ row, ]. If I am reading the code for `apply` correctly that is
because it coerced its dataframe argument to a matrix with this code:
if (is.object(X))
X <- if (dl == 2L)
as.matrix(X)
else as.array(X)
--
David.
>
> 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.
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list