[R] manipulating "by" lists and "ave()" functions
ivo welch
ivowelch at gmail.com
Sat Jul 9 00:42:46 CEST 2011
dear R wizards---more ignorance on my part, exacerbated by too few
examples in the function documentations.
> d <- data.frame( id=rep(1:3,3), x=rnorm(9), y=rnorm(9))
Question 1: how do I work with the output of "by"? for example,
> b <- by( d, d$id, function(x) coef(lm( y ~ x, data=x ) ))
> b
d$id: 1
(Intercept) x
0.2303 0.3618
---------------------------------------------------------------------------------------------------
d$id: 2
(Intercept) x
0.05785 -0.40617
---------------------------------------------------------------------------------------------------
d$id: 3
(Intercept) x
0.269 -0.378
getting the categories is easy:
> names(b)
[1] "1" "2" "3"
but how do I transform the non-name info in this by() data structure
into a matrix with dimensions 3 by 2? (presumably, there is some
vector operator that can do this kind of magic.)
Question 2: Let's say I want to add only one of the two coefficients
to d. The naive approach
a <- ave( d, d$id, FUN=function(x) coef(lm( y ~ x, data=x ))[2] )
gives me the right coefficient in each row, but overwrites every
entry. I guess I can keep only the first column of a, and add it to
d, but this seems a rather ugly and inefficient way. How is this done
better?
Question 3: repeat question 2, but keep both the intercept and the slope.
thanks in advance, as always, for any advice.
/iaw
----
Ivo Welch (ivo.welch at gmail.com)
More information about the R-help
mailing list