I have a dataframe like this:
library(tidyverse)
df = tibble(AOI = c('NA', 'NA', 'NA', 'NA', 'NA', 'EA', 'EA'),
Values = c(-1, -1, 0, 1, 1, -1, 0))
df = df %>% group_by(AOI, Values) %>% tally()
AOI Values n
<chr> <dbl> <int>
EA -1 1
EA 0 1
NA -1 2
NA 0 1
NA 1 2
And I would like to ensure that all the values -1, 0 and 1 exist for each unique AOI. As of right now the group EA
is missing 1. I would like to use I believe complete
to ensure any of the three values that are missing are automatically added and n
is filled with 0.
I am trying this:
df = df %>% complete(AOI, Values, fill = list(n = 0))
but this fails with:
Error in `dplyr::summarise()`: ! Problem while computing `..1 = complete(data = dplyr::cur_data(), ..., fill = fill, explicit = explicit)`. ℹ The error occurred in group 1: AOI = "EA". Caused by error: ! object 'AOI' not found
My expected output is this:
AOI Values n
<chr> <dbl> <int>
EA -1 1
EA 0 1
EA 1 0
NA -1 2
NA 0 1
NA 1 2
EDIT:
It doesn't have to be complete to get this task done, any way to get the desired output is fine with me.
CodePudding user response:
I assume after constantly overwriting your dataframe a few times, it may have produced an error, maybe for saving something unexpected.
*So, I changed the code a little.
Code
library(tidyverse)
df <- tibble(AOI = c('NA', 'NA', 'NA', 'NA', 'NA', 'EA', 'EA'),
Values = c(-1, -1, 0, 1, 1, -1, 0))
df %>%
arrange(AOI) %>%
count(AOI, Values) %>%
complete(AOI, Values,fill = list(n = 0))
Output
# A tibble: 6 x 3
AOI Values n
<chr> <dbl> <dbl>
1 EA -1 1
2 EA 0 1
3 EA 1 0
4 NA -1 2
5 NA 0 1
6 NA 1 2