[R] manipulating "by" lists and "ave()" functions

David Winsemius dwinsemius at comcast.net
Sat Jul 9 16:32:39 CEST 2011


On Jul 9, 2011, at 9:44 AM, Berry Boessenkool wrote:

>
>
> Maybe I'm missing something, but in what package do I find that  
> function?
>
>
>> simplify2array(b)
> Fehler: konnte Funktion "simplify2array" nicht finden
> # Function wasn't found
>
>> help.search("simplify2array")
> No help files found with alias or concept or title matching  
> ‘simplify2array’ using fuzzy matching.
>

Perhaps its new, since ?simplify2array brings up a help page and it's  
in base. Try updating.

-- David.
>
>
> A (very) short google search didn't help me either...
>
>
>> sessionInfo()
> R version 2.12.2 (2011-02-25)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252     
> LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                     
> LC_TIME=German_Germany.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] lattice_0.19-17
>
> loaded via a namespace (and not attached):
> [1] grid_2.12.2  tools_2.12.2
>
>
> Thanks ahead,
> Berry
>
>
> -------------------------------------
> Berry Boessenkool
> Potsdam
> -------------------------------------
>
>
> ----------------------------------------
>> From: ivo.welch at gmail.com
>> Date: Fri, 8 Jul 2011 16:22:18 -0700
>> To: wdunlap at tibco.com
>> CC: r-help at stat.math.ethz.ch
>> Subject: Re: [R] manipulating "by" lists and "ave()" functions
>>
>> It does!! why is this function not mentioned in the "See also"  
>> docpage for
>> "by" (and friends)??? (Also, "ave" should be mentioned there, too.)
>>
>> do I post this suggestion to add it to r-devel, or is there a way  
>> to find
>> out who is in charge of the docpage for "by"?
>>
>> regards,
>>
>> /iaw
>>
>>
>> On Fri, Jul 8, 2011 at 4:17 PM, William Dunlap <wdunlap at tibco.com>  
>> wrote:
>>
>>> Q1. simplify2array(b) gives the transpose of what
>>> I think you want.
>>>
>>> Bill Dunlap
>>> Spotfire, TIBCO Software
>>> wdunlap tibco.com
>>>
>>>> -----Original Message-----
>>>> From: r-help-bounces at r-project.org
>>>> [mailto:r-help-bounces at r-project.org] On Behalf Of ivo welch
>>>> Sent: Friday, July 08, 2011 3:43 PM
>>>> To: r-help
>>>> Subject: [R] manipulating "by" lists and "ave()" functions
>>>>
>>>> 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)
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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