Setup

# Packages
library(tidyverse)
## -- Attaching packages ------------------------------------------ tidyverse 1.2.1 --
## v ggplot2 3.0.0     v purrr   0.2.5
## v tibble  1.4.2     v dplyr   0.7.6
## v tidyr   0.8.1     v stringr 1.3.1
## v readr   1.1.1     v forcats 0.3.0
## -- Conflicts --------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

# Create a small dataset for examples
tree <- fgeo.data::luquillo_tree5_random
species <- unique(tree$sp)[1:3]
tree <- filter(tree, sp %in% species)

Data overview

table(tree$sp)
## 
## CASARB DACEXC MYRSPL 
##     94     33      9

tree
## # A tibble: 136 x 19
##    treeID stemID tag   StemTag sp    quadrat    gx    gy MeasureID CensusID
##     <int>  <int> <chr> <chr>   <chr> <chr>   <dbl> <dbl>     <int>    <int>
##  1    104    143 10009 10009   DACE~ 113      10.3  245.    439947        5
##  2    119    158 1001~ 100104  MYRS~ 1021    183.   410.    466597        5
##  3    180    225 1001~ 100174  CASA~ 921     165.   410.    466623        5
##  4   1168   1438 10131 10131   DACE~ 413      70.6  252.    440031        5
##  5   1380 114352 1015~ 149529  CASA~ 820     142.   386.    466957        5
##  6   2849   3421 1031~ 103156  CASA~ 420      79.2  389.    467249        5
##  7   3354 114750 1037~ 39879   CASA~ 220      32.0  385.    467393        5
##  8   4183   5086 10472 10472   CASA~ 1413    270.   242.    440292        5
##  9   7389 115727 10866 137800  CASA~ 1212    225.   226.    440616        5
## 10   7767 115859 1091~ 156303  CASA~ 722     132.   425.    468815        5
## # ... with 126 more rows, and 9 more variables: dbh <dbl>, pom <chr>,
## #   hom <dbl>, ExactDate <dbl>, DFstatus <chr>, codes <chr>,
## #   nostems <dbl>, status <chr>, date <dbl>

Calculate proportion by species.

  • Count all individuals by species.
tree_n <- tree %>% 
  add_count(sp) %>% 
  arrange(sp, treeID) %>% 
  select(treeID, sp, n, everything())
tree_n
## # A tibble: 136 x 20
##    treeID sp        n stemID tag   StemTag quadrat    gx    gy MeasureID
##     <int> <chr> <int>  <int> <chr> <chr>   <chr>   <dbl> <dbl>     <int>
##  1    180 CASA~    94    225 1001~ 100174  921     165.   410.    466623
##  2   1380 CASA~    94 114352 1015~ 149529  820     142.   386.    466957
##  3   2849 CASA~    94   3421 1031~ 103156  420      79.2  389.    467249
##  4   3354 CASA~    94 114750 1037~ 39879   220      32.0  385.    467393
##  5   4183 CASA~    94   5086 10472 10472   1413    270.   242.    440292
##  6   7389 CASA~    94 115727 10866 137800  1212    225.   226.    440616
##  7   7767 CASA~    94 115859 1091~ 156303  722     132.   425.    468815
##  8  10317 CASA~    94  12947 1122~ 112272  1424    261.   477.    469449
##  9  12918 CASA~    94 116633 1152~ 166501  1124    215.   461.    470017
## 10  16254 CASA~    94  20128 1190~ 119094  823     152.   445.    470847
## # ... with 126 more rows, and 10 more variables: CensusID <int>,
## #   dbh <dbl>, pom <chr>, hom <dbl>, ExactDate <dbl>, DFstatus <chr>,
## #   codes <chr>, nostems <dbl>, status <chr>, date <dbl>
  • Calculate the proportion relative to the total count of individuals.
tree_prop <- tree_n %>% 
  mutate(
    total_n = nrow(.),
    proportion = n / total_n
  ) %>% 
  select(treeID, total_n, n, proportion, everything()) %>% 
  unique() 
tree_prop
## # A tibble: 136 x 22
##    treeID total_n     n proportion sp    stemID tag   StemTag quadrat    gx
##     <int>   <int> <int>      <dbl> <chr>  <int> <chr> <chr>   <chr>   <dbl>
##  1    180     136    94      0.691 CASA~    225 1001~ 100174  921     165. 
##  2   1380     136    94      0.691 CASA~ 114352 1015~ 149529  820     142. 
##  3   2849     136    94      0.691 CASA~   3421 1031~ 103156  420      79.2
##  4   3354     136    94      0.691 CASA~ 114750 1037~ 39879   220      32.0
##  5   4183     136    94      0.691 CASA~   5086 10472 10472   1413    270. 
##  6   7389     136    94      0.691 CASA~ 115727 10866 137800  1212    225. 
##  7   7767     136    94      0.691 CASA~ 115859 1091~ 156303  722     132. 
##  8  10317     136    94      0.691 CASA~  12947 1122~ 112272  1424    261. 
##  9  12918     136    94      0.691 CASA~ 116633 1152~ 166501  1124    215. 
## 10  16254     136    94      0.691 CASA~  20128 1190~ 119094  823     152. 
## # ... with 126 more rows, and 12 more variables: gy <dbl>,
## #   MeasureID <int>, CensusID <int>, dbh <dbl>, pom <chr>, hom <dbl>,
## #   ExactDate <dbl>, DFstatus <chr>, codes <chr>, nostems <dbl>,
## #   status <chr>, date <dbl>