My data frame, gpri.l, has DBH and status of trees recorded at 188 points. Here is a sample:
Point Species DBH..cm. Live.Dead Distance..m.
1 99 Ulmus.alata 12.6 L <NA>
2 100 Blank Blank Blank <NA>
3 101 Juniperus.virginiana 29.4 L <NA>
4 101 Juniperus.virginiana 15.6 L <NA>
5 101 Juniperus.virginiana 5.2 L <NA>
6 101 Ulmus.americana 8.8 L <NA>
7 101 Ulmus.americana 4.2 L <NA>
10 102 Carya.cordiformis 22.4 L <NA>
"Blank" is for points where there were no trees. I'd like to make a table of species for each of the points. Point 99 has just Ulmus.alata, point 100 has 1 Blank, point 101 has Juniperus.virginana - 3, Ulmus.americana - 2, etc. I can make a table with structable, but it includes all 37 species, including "Blank", in each point, so the table has lots of zeros in each point, since the average point has probably less than 6 species. I've tried functions from plyr
such as
library(plyr)
ddply(gpri.l,.(Point),table(Species))
# Error in table(Species) : object 'Species' not found
ddply(gpri.l,.(Point),table("Species"))
# Error in llply(.data = pieces, .fun = .fun, ..., .progress = .progress, :
# .fun is not a function.
I don't know why those errors are occurring. I've also tried the dlply
function with the same results.
I can't seem to get apply
and table
to work together. I've tried to think about how I could subset or split the data frame by Points, but that seems to me that it would involve writing some kind of loop. I spent a whole day trying to write a loop and I couldn't even get the Points to populate a vector set up to receive them. I would really appreciate some help with making the tables. And explanation of my plyr
errors too.
EDIT after Henrik's answer--
@Henrik I tried your first suggestion and it didn't work for me like it did for you. Your comment shows what I was looking for, but I've copied and pasted what it looks like for me.
> h.h<-dlply(gpri.l, .(Point), function(x) table(x$Species))
> head(h.h)
$`99`
Blank Carya.cordiformis Carya.illinoisensis Carya.sp
0 0 0 0
Carya.texana Carya.tomentosa Celtis.laevigata Celtis.occidentalis
0 0 0 0
Celtis.sp. Cercis.canadensis Cornus.sp. Diospyros.virginiana
0 0 0 0
Fraxinus.pensylvanica Fraxinus.sp. Gleditsia.triacanthos Juglans.nigra
0 0 0 0
Juniperus.virginiana Maclura.pomifera Morus.sp Plantanus.occidentalis
0 0 0 0
Prunus.americana Prunus.serotina Prunus.sp. Quercus.macrocarpa
0 0 0 0
Quercus.marilandica Quercus.muhlenbergii Quercus.rubra Quercus.sp
0 0 0 0
Quercus.stellata Quercus.velutina Robinia.pseudoacacia Sapindus.saponaria
0 0 0 0
Sassafras Sideroxylon.lanigosa Ulmus.alata Ulmus.americana
0 0 1 0
$`100`
Blank Carya.cordiformis Carya.illinoisensis Carya.sp
1 0 0 0
Carya.texana Carya.tomentosa Celtis.laevigata Celtis.occidentalis