1

I'm running into an issue passing a column being created in mutate to the the group_by function.

here is some example data:

set.seed(42)  ## for sake of reproducibility

w2_test <- data.frame(id=1:607, 
                      wave = "W2",
                      tech =floor(runif(607, 1, 6)))

This function does recoding of Likert scales

recoding_1_fn <- function(var_name) {
  var_name_ <- case_when(
    var_name %in% c(1, 2, 3, 6) ~ "disagree",
    var_name %in% c(4, 5) ~ "agree",
    TRUE ~ NA
  )
}

and this function attempts to summarize n by the recoded scales above

summary_w2_fn <- function(df, w2_name, w3_name, suffix = "recoded") {
  
  df %>% 
    rename({{w3_name}} := {{w2_name}}) %>%
    count(wave, {{w3_name}}) %>%
    drop_na() %>% 
    mutate("{{w3_name}}_{suffix}" := recoding_1_fn({{w3_name}})) %>% 
    group_by(wave, "{{w3_name}}_{suffix}") %>%
    summarise(n = sum(n)) %>% 
    rename("{{w3_name}}" := "{{w3_name}}_{suffix}")
}

when called This above function works up until the mutate function

w2 %>% summary_w2_fn(tech, W3_SUS_4)

and I get a dataset that looks like this:

df up until mutate

If I let it run to the group_by, it won't recognize the column "{{w3_name}}_{suffix}" and I get the following

df up until summarise

I've tried a number of enquo, quos, !!, etc. but really at random, since I'm stuck, nothing has worked.

If there a way that I can pass the column being created in mutate to the next function?

You help appreciated.

Thanks

Alex Braga
  • 23
  • 6
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) – MrFlick Mar 31 '23 at 20:17
  • This is a tidy evaluation problem: in your second screenshot, notice that the column name includes double quotes. Read up on NSE in the tidyverse. Also, I suspect, from the use of `_suffix`, that your data frame is not tidy. If so, your life in the tidyverse is always going to be more difficult than it needs to be. – Limey Mar 31 '23 at 20:50

1 Answers1

4

Untested since you didn't provide a reprex:

summary_w2_fn <- function(df, w2_name, w3_name, suffix = "recoded") {
  recoded_name <- rlang::englue("{{w3_name}}_{suffix}")
  
  df %>% 
    rename({{w3_name}} := {{w2_name}}) %>%
    count(wave, {{w3_name}}) %>%
    drop_na() %>% 
    mutate("{recoded_name}" := recoding_1_fn({{w3_name}})) %>% 
    group_by(wave, .data[[recoded_name]]) %>%
    summarise(n = sum(n))
}

I removed the last expression because it had the structure of a filter() but it was a call to rename(), something looks wrong.

Lionel Henry
  • 6,652
  • 27
  • 33
  • Amazing @Lionel. when I add back the rename function as rename({{w3_name}} := .data[[recoded_name]]) I get the warning: Use of .data in tidyselect expressions was deprecated in tidyselect 1.2.0. ℹ Please use `all_of(var)` (or `any_of(var)`) instead of `.data[[var]]`. How do we address that? – Alex Braga Mar 31 '23 at 21:28
  • 1
    You can use `all_of(var)` like the message suggests. I guess another operator for single variables would be better here, but we don't have one. – Lionel Henry Mar 31 '23 at 22:18