I have a dataframe:
Store <- c('A','A','A', 'B','C','C')
Main_or_branch <- c('main', 'Branch','Branch','main','main', 'Branch')
flavor <- c('chocolate', 'N/A','N/A', 'vanilla', 'strawberry','N/A')
df <- data.frame(Store, Main_or_branch, flavor)
and it looks like this:
Store Main_or_branch flavor
<fct> <fct> <fct>
1 A main chocolate
2 A Branch N/A
3 A Branch N/A
4 B main vanilla
5 C main strawberry
6 C Branch N/A
I'm trying to use group by and fill :
df %>% group_by(Store) %>% fill(flavor)
but my output still looks like this
Store Main_or_branch flavor
<fct> <fct> <fct>
1 A main chocolate
2 A Branch N/A
3 A Branch N/A
4 B main vanilla
5 C main strawberry
6 C Branch N/A
Does anyone know what the issue could be? running version tidyr 1.1.4 Thanks!
CodePudding user response:
"N/A" is not the same as NA
. Here you go:
df[df == "N/A"] <- NA # Substitute all instances of "N/A" with NA
df %>% group_by(Store) %>% fill(flavor)
# A tibble: 6 x 3
# Groups: Store [3]
Store Main_or_branch flavor
<chr> <chr> <chr>
1 A main chocolate
2 A Branch chocolate
3 A Branch chocolate
4 B main vanilla
5 C main strawberry
6 C Branch strawberry
CodePudding user response:
Just need to convert the string NAs to actual NA
.
df %>%
mutate(
flavor = na_if(flavor, 'N/A')
) %>%
group_by(Store) %>%
tidyr::fill(flavor)
CodePudding user response:
Using na.locf
library(zoo)
library(dplyr)
df %>%
group_by(Store) %>%
mutate(flavor = na.locf(na_if(flavor, "N/A"))) %>%
ungroup
# A tibble: 6 × 3
Store Main_or_branch flavor
<chr> <chr> <chr>
1 A main chocolate
2 A Branch chocolate
3 A Branch chocolate
4 B main vanilla
5 C main strawberry
6 C Branch strawberry