Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure repeated levels of recode_shadow works #273

Merged
merged 3 commits into from
Sep 23, 2020

Conversation

njtierney
Copy link
Owner

Description

This PR adds a catch condition for shadow_expand_relevel, which is called by recode_shadow to create the extra special missing levels.

Related Issue

Should fix #272

Example

library(tidyverse)
library(naniar)

df_sh <- data.frame(Q1 = c("yes", "no", "no", NA), 
                 Q2 = c("a", NA, NA, NA), 
                 Q3 = c(1, NA, NA, 4)) %>% 
  mutate(Q1 = factor(Q1)) %>% 
  mutate(Q2 = factor(Q2)) %>% 
  nabular()

# Q1 is a filter question - people who answer no should skip Q2 and Q3
df_sh_recode_q2 <- df_sh %>% 
  recode_shadow(Q2 = .where(Q1 %in% "no" ~ "skip"))

df_sh_recode_q3 <- df_sh_recode_q2 %>% 
  recode_shadow(Q3 = .where(Q1 %in% "no" ~ "skip"))

# there are empty factor levels ..
df_sh_recode_q3$Q1_NA %>% table(., useNA = "always")
#> .
#>     !NA      NA NA_skip    <NA> 
#>       3       1       0       0
df_sh_recode_q3$Q2_NA %>% table(., useNA = "always")
#> .
#>     !NA      NA NA_skip    <NA> 
#>       1       1       2       0
df_sh_recode_q3$Q3_NA %>% table(., useNA = "always")
#> .
#>     !NA      NA NA_skip    <NA> 
#>       2       0       2       0

Created on 2020-09-22 by the reprex package (v0.3.0)

Tests

Yes

NEWS + DESCRIPTION

@njtierney njtierney merged commit e8c7bc4 into master Sep 23, 2020
@njtierney njtierney deleted the recode-shadow-factor-bug branch September 23, 2020 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

naniar::recode_shadow() throwing error: "idx must contain one integer for each level of f"
1 participant