[R] help with aggregate()

Sam Steingold sds at gnu.org
Tue Feb 15 00:42:40 CET 2011


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.."

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.

-- 
Sam Steingold (http://sds.podval.org/) on CentOS release 5.3 (Final)
http://dhimmi.com http://camera.org http://palestinefacts.org
http://memri.org http://jihadwatch.org http://ffii.org http://pmw.org.il
There are 10 kinds of people: those who count in binary and those who do not.



More information about the R-help mailing list