[R] creating table of averages

Duncan Murdoch murdoch at stats.uwo.ca
Tue Sep 9 20:27:06 CEST 2008


On 9/9/2008 2:12 PM, Adam D. I. Kramer wrote:
> Maybe something like this:
> 
> by(df[,c(77,81,86,90,94,98,101,106)],df$category,apply,2,mean)
> 
> ...which would then need to be reformatted into a data frame (there is
> probably an easy way to do this which I don't know).

sparseby() in the reshape package is more flexible than by(). If the 
function returns a vector with a consistent length, you'll get a 
dataframe with columns corresponding to its entries.

Duncan Murdoch

> 
> aggregate seems like a more reasonable choice, but the function for
> aggregate must return scalars, not rows...tapply doesn't take data.frame
> inputs. Maybe someone else has a suggestion?
> 
> --Adam
> 
> On Tue, 9 Sep 2008, Lawrence Hanser wrote:
> 
>> Dear Colleagues,
>>
>> I have a dataframe with variables:
>>
>>  [1] "ID"             "category"       "a11"            "a12"
>> "a13"            "a21"
>>  [7] "a22"            "a23"            "a31"            "a32"
>> "b11"            "b12"
>> [13] "b13"            "b21"            "b31"            "b32"
>> "b33"            "b41"
>> [19] "b42"            "c11"            "c12"            "c21"
>> "c22"            "c23"
>> [25] "c31"            "c32"            "c33"            "d11"
>> "d12"            "d13"
>> [31] "d14"            "d21"            "d22"            "d23"
>> "d24"            "d25"
>> [37] "d31"            "d32"            "d33"            "e11"
>> "e12"            "e13"
>> [43] "e21"            "e22"            "e23"            "e31"
>> "e32"            "e33"
>> [49] "f11"            "f12"            "f13"            "f14"
>> "f21"            "f22"
>> [55] "f23"            "f24"            "g11"            "g12"
>> "g13"            "g14"
>> [61] "g21"            "g22"            "g23"            "g24"
>> "g31"            "g32"
>> [67] "g33"            "g41"            "g42"            "g43"
>> "h11"            "h12"
>> [73] "h13"            "h21"            "h22"            "h23"
>> "C1.Employ"      "SC11.Ops"
>> [79] "SC12.Unit"      "SC13.Nonadvers" "C2.Enterprise"  "SC21.Structure"
>> "SC22.Gov"       "SC23.Culture"
>> [85] "SC24.Stratcomm" "C3.Manage"      "SC31.Resource"  "SC32.Change"
>> "SC33.Continue"  "C4.Stratthink"
>> [91] "SC41.Vision"    "SC42.Decision"  "SC43.Adapt"     "C5.Lead"
>> "SC51.Develop"   "SC52.Care"
>> [97] "SC53.Diversity" "C6.Foster"      "SC61.Teams"     "SC62.Negotiate"
>> "C7.Embody"      "SC71.Ethical"
>> [103] "SC72.Follower"  "SC73.Warrior"   "SC74.Develop"   "C8.Comm"
>> "C81.Speak"      "C82.Listen"
>> [109] "OverallImp"
>>
>> The variable "category" has four values: Regular, CCM, CFM, and Other
>>
>> I'd like to create a table like this to feed into barplot2:
>>
>> row.name  C1.Employ C2.Enterprise  C3.Manage  C4.Stratthink  C5.Lead
>> C6.Foster  C7.Embody  C8.Comm
>> Regular         3.68              4.27             3.22
>> etc......
>> CCM             4.32              4.56              etc.....
>> CFM              etc.........
>> Other             etc.........
>>
>> So far, I have been able to get this far:
>>
>> >
>> mean(subset(impchiefs08,category=="Regular",select=c(C1.Employ,C2.Enterprise,C3.Manage,C4.Stratthink,C5.Lead,C6.Foster,C7.Embody,C8.Comm
>> )))
>>    C1.Employ C2.Enterprise     C3.Manage C4.Stratthink       C5.Lead
>> C6.Foster     C7.Embody       C8.Comm
>>     3.600000      3.851111      4.482222      4.346667      4.608889
>> 4.444444      4.602222      4.493333
>>>
>>
>> But I am stumped as to how to get what I want.
>>
>> Thanks in advance.
>>
>> Larry
>>
>> 	[[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.



More information about the R-help mailing list