[R] help with aggregate()

Sam Steingold sds at gnu.org
Tue Feb 15 19:47:52 CET 2011


> * Jan van der Laan <euryc at rbbf.qqf.ay> [2011-02-15 10:09:44 +0100]:
>
> The fact that your column names from your aggregate result contain
> multiple numbers, suggests that something has gone wrong with reading
> your data in from file. Have you had a look at your data.frame 'all'?

yes, the data is loaded correctly.

> Are BAR and X etc. numeric? Judging from the 'c. etc' they aren't.

they are.
the numbers after "c." are the actual values in those columns.

when I limited the file to a few lines, the aggregation result is:

  c.33879300..3713870..190963000..190963000..33879300..190963000..
1                                                        190963000
2                                                         33879300
3                                                          3713870
  c.1.37087599544937..1.72690992018244..1.82034830430797..1.82034830430797..
1                                                                   1.820348
2                                                                   1.370876
3                                                                   1.726910

which is correct except for the title.
(actually, the numbers I aggregate by "mean" are all the same for each
factor, so they are repeated in the title)

> However, you didn't use names arguments in you list() so R
> will generate names for you. Hence, the strange names.

when I pass "names = list(FOO,BAR,QUUX,X.Y)" I still get the bad names.
I had to manually do
names(byFOO) <- list("FOO","BAR","QUUX","X.Y");

> aggregate returns a data.frame. So if you want to do combine more than
> one aggregate call, you can use merge to merge the results:
>
> Count<- aggregate(all$FOO, by = list(FOO=all$FOO), FUN = length);
> byFOO<- merge(byFOO, by="FOO")

Error in is.null(x) : 'x' is missing

looks like TRT is:

byFOO$Count <- aggregate(all$FOO, by = list(all$FOO), FUN = length)$x;

(note "$x" at the end).

right?

>>  How do I rename a column?
>
> ?names
>
> e.g.
> names(all)<- c("column1" , "column2", ...)

thus I have to re-list all the existing columns.
is it possible to write something like

names(all)[7] <- "myname"

thanks a lot for your help!

PS. neither sort, nor order appear to sort my data frame by a specific column....

-- 
Sam Steingold (http://sds.podval.org/) on CentOS release 5.3 (Final)
http://palestinefacts.org http://truepeace.org http://honestreporting.com
http://dhimmi.com http://jihadwatch.org http://camera.org http://ffii.org
C combines the power of assembler with the portability of assembler.



More information about the R-help mailing list