hadley / plyr

A R package for splitting, applying and combining large problems into simpler problems
plyr.had.co.nz
Other
494 stars 117 forks source link

Special variables 'i' and 'n' in d_ply functions used with summarise/mutate #272

Open tzoltak opened 8 years ago

tzoltak commented 8 years ago

It seems that when summarise() or mutate() is used along with d.ply functions there are two special variables: 'n' and 'i' that describe overall number of groups and consecutive number of group. These variables mask global variables of the same names which someone pottentialy may want to use in formulas to construct new variables with summarise() or mutate(). I know that's rather unusual (perhaps even quirky) case and I don't advocate for change of this behavior. However it would be good idea to mention about it in documentation of summarise() and mutate().

crowding commented 8 years ago

?here Try ddply(df, here(mutate), ...). Or dplyr :)

On Wed, Mar 9, 2016 at 1:05 AM, tzoltak notifications@github.com wrote:

It seems that when summarise() or mutate() is used along with d.ply functions there are two special variables: 'n' and 'i' that describe overall number of groups and consecutive number of group. These variables mask global variables of the same names which someone pottentialy may want to use in formulas to construct new variables with summarise() or mutate(). I know that's rather unusual (perhaps even quirky) case and I don't advocate for change of this behavior. However it would be good idea to mention about it in documentation of summarise() and mutate().

— Reply to this email directly or view it on GitHub https://github.com/hadley/plyr/issues/272.

tzoltak commented 8 years ago

Thanks, but you missed my point. When you know that d.ply() with summarise()/mutate() do such a thing, there are many simple ways to make your code working as you wish. The problem is, that it is rather hard to find out, what causes your code working wrong when this behavior isn't documented.