[R] problem with as.data.frame.table
Tord Snall
tord.snall at ebc.uu.se
Thu Jan 16 18:47:03 CET 2003
Dear all,
I think that what I want is an as.data.frame.table-object, but see error
message below.
I have a data frame with one tree per row, diaclass tells if it is a small,
mid or large tree
> cpy.tradart[1:5, ]
ObjektID diaclass
1 AX.Grb.1 bigdia
2 AX.Grb.1 middia
3 AX.Grb.1 middia
4 AX.Grb.1 smalldia
5 AX.Grb.1 middia
>
I want a data frame telling no of trees per diameter class, per ObjektID
There are 107 ObjektID
> nlevels(cpy.tradart$ObjektID)
[1] 107
and 3 diaclass levels
> levels(cpy.tradart$diaclass)
[1] "bigdia" "middia" "smalldia"
I first split the data frame
> group <- split(cpy.tradart$diaclass, list(cpy.tradart[, c("ObjektID",
"diaclass")])) #
>
and then sapply
> test <- sapply( group, function(x) sum(length(x)))
>
> # This looks good
> test[1:5]
AX.Grb.O.bigdia S.1.bigdia S.1.ominv.bigdia S.10.bigdia
S.11.bigdia
6 3 3 11
5
>
> # But not each diaclass is represented in each ObjektID, because
> length(test)
[1] 313
> # is smaller than 3*107
and I cannot split up the names of each element of the vector "test".
# What I want is a data frame like this:
ObjectID diaclass Frequency
AX.Grb.1 smalldia 23
AX.Grb.1 middia 45
AX.Grb.1 bigdia 0
AX.Grb.2 smalldia 2
AX.Grb.2 middia 5
AX.Grb.2 bigdia 10
AX.Grb.3 smalldia 3
AX.Grb.3 middia 45
AX.Grb.3 bigdia 0
Note that there will be zeroes, but that information is lacking in "test".
I have also tried:
> as.data.frame.table(cpy.tradart)
Error in as.table.default(x) : cannot coerce into a table
Could someone please give a hint.
Thanks in advance!!
Sincerely,
Tord
-----------------------------------------------------------------------
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