[R] Grouping columns

David Winsemius dwinsemius at comcast.net
Wed Jul 20 17:36:20 CEST 2011


On Jul 20, 2011, at 10:42 AM, Geophagus wrote:

> *Hi @ all,
> I have a question concerning the possibilty of grouping the columns  
> of a
> matrix.
> R groups the columns alphabetically.
> What can I do to group the columns in my specifications?

Dear Earth Eater;

You can create a factor whose levels are ordered to your  
specification. Your columns: "umweltkompartiment" obviously has those  
levels. This might also offer advantages in situations where there was  
not complete representation of all levels in all the files

So your tapply() calls could have been of this form:

b1_1<-tapply(b1$betriebs_id,
                  factor( b1$umweltkompartiment, levels=
                     c("Gesamt", "Wasser", "Boden", "Luft", "Abwasser",
                       "Gefährliche Abfälle", "nicht gefährliche  
Abfälle") )
                ,length)
# code would e more compact if you created a facvtor vector and use it  
as an argument to factor:

faclevs <- c("Gesamt", "Wasser", "Boden", "Luft", "Abwasser",
                       "Gefährliche Abfälle", "nicht gefährliche  
Abfälle")
b1_1<-tapply(b1$betriebs_id,
                  factor( b1$umweltkompartiment, levels= faclev )
                ,length)
<< lather, rinse, repeat x 3>>
-- 
David.
>
> The script is the following:*
>
>> #R-Skript: Anzahl xyz
>>
>> #Quelldatei einlesen
>> b<-read.csv2("Z:/int/xyz.csv", header=TRUE)
>>
>> #Teilmengen für die Einzeljahre generieren
>> b1<-subset(b,jahr=="2007")
>> b2<-subset(b,jahr=="2008")
>> b3<-subset(b,jahr=="2009")
>>
>> #tapply für die Einzeljahre auf die jeweilige BranchenID
>> b1_1<-tapply(b1$betriebs_id,b1$umweltkompartiment,length)
>> b1_2<-tapply(b2$betriebs_id,b2$umweltkompartiment,length)
>> b1_3<-tapply(b3$betriebs_id,b3$umweltkompartiment,length)
>>
>> #Verbinden der Ergebnisse
>> b11<-rbind(b1_1,b1_2,b1_3)
>> Gesamt<-apply(X=b11,MARGIN=1, sum)
>> b13<-cbind(Gesamt,b11)
>> b13
>     Gesamt Abwasser Boden Gefährliche Abfälle Luft nicht gefährliche
> Abfälle Wasser
> b1_1   9832      432    18                3147 2839
> 1592   1804
> b1_2  10271      413    28                3360 2920
> 1715   1835
> b1_3   9983      404    21                3405 2741
> 1691   1721
>
> *Now I want to have the following order of the columns:
> Gesamt, Wasser, Boden, Luft, Abwasser, Gefährliche Abfälle, nicht
> gefährliche Abfälle
>
> Thanks a lot for your answers!
> Fak*
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Grouping-columns-tp3681018p3681018.html
> Sent from the R help mailing list archive at Nabble.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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list