Alexander Shenkin
2012-Apr-02 15:58 UTC
[R] summaryBy: transformed variable on RHS of formula?
Hi Folks,
I'm trying to cut my data inside the summaryBy function. Perhaps
formulas don't work that way? I'd like to avoid adding another column
if possible, but if I have to, I have to. Any ideas?
Thanks,
Allie
require(doBy)
df = dataframe(a <- rnorm(100), b <-rnorm(100))
summaryBy(a ~ cut(b,c(-100,-1,1,100)), data=df) # preferred
solution, but it throws an error
# Error in
`[.data.frame`(data, , rh.var, drop = FALSE) : undefined columns selected
df$c = cut(b,c(-100,-1,1,100))
summaryBy(a ~ c, data=df) # works fine
Søren Højsgaard
2012-Apr-02 16:07 UTC
[R] summaryBy: transformed variable on RHS of formula?
Sorry, no - you will have to do something like the latter suggestion.
Regards
S?ren
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Alexander Shenkin
Sent: 2. april 2012 17:59
To: r-help at r-project.org
Subject: [R] summaryBy: transformed variable on RHS of formula?
Hi Folks,
I'm trying to cut my data inside the summaryBy function. Perhaps formulas
don't work that way? I'd like to avoid adding another column if
possible, but if I have to, I have to. Any ideas?
Thanks,
Allie
require(doBy)
df = dataframe(a <- rnorm(100), b <-rnorm(100))
summaryBy(a ~ cut(b,c(-100,-1,1,100)), data=df) # preferred solution, but it
throws an error
# Error in
`[.data.frame`(data, , rh.var, drop = FALSE) : undefined columns selected
df$c = cut(b,c(-100,-1,1,100))
summaryBy(a ~ c, data=df) # works fine
______________________________________________
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.
Gabor Grothendieck
2012-Apr-02 16:08 UTC
[R] summaryBy: transformed variable on RHS of formula?
On Mon, Apr 2, 2012 at 11:58 AM, Alexander Shenkin <ashenkin at ufl.edu> wrote:> Hi Folks, > > I'm trying to cut my data inside the summaryBy function. ?Perhaps > formulas don't work that way? ?I'd like to avoid adding another column > if possible, but if I have to, I have to. ?Any ideas? > > Thanks, > Allie > > ? ?require(doBy) > ? ?df = dataframe(a <- rnorm(100), b <-rnorm(100)) > ? ?summaryBy(a ~ cut(b,c(-100,-1,1,100)), data=df) # preferred > solution, but it throws an error > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# Error in > `[.data.frame`(data, , rh.var, drop = FALSE) : undefined columns selected > > ? ?df$c = cut(b,c(-100,-1,1,100)) > ? ?summaryBy(a ~ c, data=df) ? ? ? # works fine >Try this: summaryBy(a ~ b, data = transform(df, b = cut(b, c(-100, -1, 1, 100)))) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com