[R] [plyr] Question regarding ddply: use of .(as.name(varname)) and varname in ddply function

Hadley Wickham hadley at rice.edu
Tue Dec 7 03:12:08 CET 2010


It's easiest to see what's going on if you use eval.quoted directly:

eval.quoted(.(cyl), mtcars)
eval.quoted(.("cyl"), mtcars)
eval.quoted(.(as.name("cyl")), mtcars)

But you shouldn't need to do any syntactic hackery because the default
method automatically parses the string for you:

eval.quoted(as.quoted("cyl"), mtcars)

Hadley

On Mon, Dec 6, 2010 at 6:22 PM, Sunny Srivastava
<research.baba at gmail.com> wrote:
> Hi Hadley:
> I was trying to use ddply using the format . (var1) for splitting.
> I thought . ( as.name(grp) ) would do the same thing. But it does not. I was
> just trying to know my mistake. I am sorry if it is a basic question.
> Thank you and others for your reply.
> Best Regards,
> S.
>
> On Mon, Dec 6, 2010 at 5:28 PM, Hadley Wickham <hadley at rice.edu> wrote:
>>
>> On Mon, Dec 6, 2010 at 3:58 AM, Sunny Srivastava
>> <research.baba at gmail.com> wrote:
>> > Dear R-Helpers:
>> >
>> > I am using trying to use *ddply* to extract min and max of a particular
>> > column in a data.frame. I am using two different forms of the function:
>> >
>> >
>> > ## var_name_to_split is a string -- something like "var1" which is the
>> > name
>> > of a column in data.frame
>> >
>> > ddply( df, .(as.name(var_name_to_split)), function(x) c(min(x[ , 3] ,
>> > max(x[
>> > , 3]))) ## fails with an error - case 1
>> > ddply( df, var_name_to_split , function(x) c(min(x[ , 3] , max(x[ ,
>> > 3])))
>> >               ## works fine - case 2
>> >
>> > I can't understand why I get the error in case 1. Can someone help me
>> > please?
>>
>> Why do you expect case 1 to work?
>>
>> Hadley
>>
>> --
>> Assistant Professor / Dobelman Family Junior Chair
>> Department of Statistics / Rice University
>> http://had.co.nz/
>
>



-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list