I have a CSV with three columns: a Plot
, an integer vector, Species
, a character vector, and DBHClass
, an integer vector. The CSV looks like this:
##Read in table
library(tidyverse)
PlotData <- tibble(Plot = c(1, 2, 2, 3, 3, 4, 4),
Species = c("SNAG", "BLC", "AEL", "BASS", "REDO", "REDM", "BASS"),
DBHClass = c(7, 11, 7, 9, 15, 17, 13)) %>%
print()
Before I reach my issue, I used cut_width()
on DBHClass
to make the column appear as so and added another column, Exp.ac
:
#using cut_width()
PlotData <- PlotData %>%
mutate(DBH2in = cut_width(PlotData$DBHClass, width = 2),
Exp.ac = 20) %>%
group_by(Plot, Species, DBH2in) %>%
print()
I want to use the complete()
function to extrapolate this table so that every unique observation in the Species
column is represented in every unique observation in the Plot
column. From length(unique(PlotData$Species))
, there are 7 Species
that I want to have represented in all unique observations of DBHClass
within all 4 Plot
's for a total of 168 rows, hence group_by(Plot, Species, DBH2in)
. The recommended code I have for the next step is as follows, however, this error is thrown on row 1:
#using complete()
PlotData <- PlotData %>%
complete(Plot, Species, DBH2in,
fill = list(Exp.ac = 0, DBHClass = 0)) %>%
group_by(Plot, Species, DBH.2in)
Error in `dplyr::summarise()`:
! Problem while computing `..1 = complete(data = dplyr::cur_data(), ..., fill = fill, explicit = explicit)`.
ℹ The error occurred in group 1: Plot = 1, Species = "SNAG", DBH2in = "[7,9]".
Caused by error:
! object 'Plot' not found
To circumvent this, I tried using do(complete))
and slighly different arguments. I've found the following argument doesn't throw an error:
#using do(complete())
PlotData<- PlotData%>%
group_by(Plot, Species, DBH2in) %>%
do(complete(., Plot, Species, DBH2in,
fill = list(Exp.ac = 20,
DBHClass = 0))) %>%
group_by(Plot, Species, DBH2in) %>%
print()
But this only returns 35 rows, and in Plot 1 for example, the only Species that is shown is shown is Species = "SNAG"
. What could be causing complete()
to throw this error? I've tried naming Plot
as an object (because that's what I inferred it wanted) and removed Plot
to see if the column was an incorrect class, but the same error gets thrown saying that Species
is not found. Using other related functions like nesting()
and expand()
haven't been successful either. Any suggestions??
Thanks!