[R] help with aggregate()

Peter Ehlers ehlers at ucalgary.ca
Tue Feb 15 10:41:13 CET 2011


On 2011-02-14 15:42, Sam Steingold wrote:
> Hi,
>
> I am trying to aggregate some data and I am confused by the results.
> I load a data frame "all" from a csv file, and then I do:
> (FOO,BAR,X,Y come from the header line in the csv file,
> BTW, how do I rename a column?)
>
> byFOO<- aggregate(list(all$BAR,all$QUUX,all$X/all$Y),
>                       by = list(FOO=all$FOO),
>                       FUN = mean);
>
> I expect a data frame with 4 columns: FOO,BAR,QUUX and X/Y with all FOO
> being different (they are character strings, do I need a special
> incantation to turn them into factors?)
> what I get is indeed a data frame but with names
>
> [1] "FOO"
> [2] "c.1.78e.11..4.38e.09..1.461e.11..4.3186e.10..1.1181e.10..5.5389e.10.."
> [3] "c.33879300..3713870..190963000..7042170..4590010..91569200..12108200.."
> [4] "c.1.37087599544937..1.72690992018244..1.82034830430797..1.70338983050847.."

I think that all you need is to provide names in your
aggregate() call:

  byFOO <- aggregate(list(V1 = all$BAR, V2 = ....)

For renaming variables in the dataframe any time,
see help(names).

Peter Ehlers

>
> why? how do I fix the column names?
>
> then I am trying to add to that same frame byFOO some other columns:
>
> byFOO$Count<- aggregate(all$FOO, by = list(all$FOO), FUN = length);
> byFOO$Mean<- aggregate(all$Value, by = list(all$FOO), FUN = mean);
> byFOO$Total<- aggregate(all$Value, by = list(all$FOO), FUN = sum);
>
> however, byFOO$Count et al are not columns in byFOO with the appropriate
> names ("Count"&c) but data frames with columns "Group.1" and "x".
> Luckily, at least it appears that byFOO$Count$Group.1 is the same as
> byFOO$FOO, as they should be, although I don't see any function which
> would check that two vectors are the same ("==" returns a vector which I
> have to manually inspect for presence of "FALSE").
>
> So, how do I aggregate the data frame?
> How do I rename a column?
> How do I check that two vectors are the same?
>
> thanks a lot!
>
> PS. I have not used R for a few years, so please be gentle...
> PPS. Please do not tell me to RTFM - I did. At least tell me what to
> search for.
>



More information about the R-help mailing list