2

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 
shea
  • 528
  • 4
  • 17
  • Your 'Species' variable is most likely a `factor`. Convert it to `character` (`gpri.l$Species <- as.character(gpri.l$Species)`) and try `dlply` again. – Henrik Jan 06 '15 at 23:24

1 Answers1

2

If you wish to return your tree counts at each point as class table, you need to use dlply with an anonymous function. This will result in a list with one element per point, each containing a table:

dlply(df, .(Point), function(x) table(x$Species))
# $`99`
# 
# Ulmus.alata 
#           1 
# 
# $`100`
# 
# Blank 
#     1 
# 
# $`101`
# 
# Juniperus.virginiana      Ulmus.americana 
#                    3                    2 
# 
# $`102`
# Carya.cordiformis 
#                 1

On the other hand, if you are happy to store your counts in a data frame (possibly easier to use than tables), you may use ddply and summarise; group by 'Point' and 'Species', and calculate counts of each 'Species' with length:

ddply(df, .(Point, Species), summarise, count = length(Species))
#   Point              Species count
# 1    99          Ulmus.alata     1
# 2   100                Blank     1
# 3   101 Juniperus.virginiana     3
# 4   101      Ulmus.americana     2
# 5   102    Carya.cordiformis     1

You may also have a look at the equivalent dplyr code:

library(dplyr)
df %>%
  group_by(Point, Species) %>%
  summarise(count = n())
#   Point              Species count
# 1    99          Ulmus.alata     1
# 2   100                Blank     1
# 3   101 Juniperus.virginiana     3
# 4   101      Ulmus.americana     2
# 5   102    Carya.cordiformis     1
Henrik
  • 65,555
  • 14
  • 143
  • 159