[R] aggregate: "sum" not meaningful for factors

Tord Snall tord.snall at ebc.uu.se
Thu Jan 2 14:45:03 CET 2003


Dear all,
I try to summarise my data per category using aggregate, but for some
reason I get the error message "sum" not meaningful for factors even though
my vector is numeric. The data set is shown below.

Could someone please give a hint.

Thanks in advance!

Sincerely,
Tord

> names(test)
[1] "ObjektID"     "tallstubbyta"

> is.factor(test$ObjektID); is.factor(test$tallstubbyta)
[1] TRUE
[1] FALSE

> is.numeric(test$ObjektID); is.numeric(test$tallstubbyta)
[1] FALSE
[1] TRUE

> mean(test$tallstubbyta)
[1] 0.01142584

> aggregate(test, list(test$ObjektID), length)[, c("Group.1", "tallstubbyta")]
  Group.1 tallstubbyta
1     S.1           19
2    S.10            4

> aggregate(test, list(test$ObjektID), mean)[, c("Group.1", "tallstubbyta")]
  Group.1 tallstubbyta
1     S.1   0.01383128
2    S.10   0.00000000
Warning messages: 
1: argument is not numeric or logical: returning NA in:
mean.default(X[[1]], ...) 
2: argument is not numeric or logical: returning NA in:
mean.default(X[[2]], ...) 

> aggregate(test, list(test$ObjektID), sum)[, c("Group.1", "tallstubbyta")]
Error in Summary.factor(..., na.rm = na.rm) : 
        "sum" not meaningful for factors

> aggregate
function (x, ...) 
UseMethod("aggregate")

> test
   ObjektID tallstubbyta
1       S.1 0.0000000000
2       S.1 0.0000000000
3       S.1 0.0000000000
4       S.1 0.0000000000
5       S.1 0.0000000000
6       S.1 0.1320254313
8       S.1 0.0003141593
9       S.1 0.0000000000
10      S.1 0.0003141593
11      S.1 0.0003141593
12      S.1 0.0530929158
13      S.1 0.0000000000
14      S.1 0.0000000000
15      S.1 0.0003141593
16      S.1 0.0000000000
17      S.1 0.0226980069
18      S.1 0.0003141593
19      S.1 0.0003141593
20      S.1 0.0530929158
21     S.10 0.0000000000
22     S.10 0.0000000000
26     S.10 0.0000000000
27     S.10 0.0000000000
> 
> 


-----------------------------------------------------------------------
Tord Snäll
Avd. f växtekologi, Evolutionsbiologiskt centrum, Uppsala universitet
Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University
Villavägen 14			
SE-752 36 Uppsala, Sweden
Tel: 018-471 28 82 (int +46 18 471 28 82) (work)
Tel: 018-25 71 33 (int +46 18 25 71 33) (home)
Fax: 018-55 34 19 (int +46 18 55 34 19) (work)
E-mail: Tord.Snall at ebc.uu.se
Check this: http://www.vaxtbio.uu.se/resfold/snall.htm!
------------------------------------------------------------------------




More information about the R-help mailing list