tidyselect::where() inconsistencies: where is where()?

Summary: You can do rename(A=1, B=2), can you do the same using rename_with()? my ~str_replace(... paste0()) works, I don't need to change that. But it only works for one variable at a time. Tidyselect suggests wrapping where(~str_replace...) but then complains it can't find it even though I can get where() to work in other instances.

I want to implement rename_with for more than one variable, but I get an error Error: Formula shorthand must be wrapped in where()`.

# Bad
  data %>% select(~str_replace(., "Var_2_", paste0("Issue: Time")))

  # Good
  data %>% select(where(~str_replace(., "Var_2_", paste0("Issue: time"))))

Example original: test%>% rename_with( ~str_replace(., "Var_2_", paste0("Issue: Time")), ~str_replace(., "Var_3_", paste0("Issue: Time")))

when I run test%>% rename_with(where( ~str_replace(., "Var_2_", paste0("Issue: Time")), ~str_replace(., "Var_3_", paste0("Issue: Time"))))

and test%>% rename_with( where(~str_replace(., "Var_2_", paste0("Issue: Time"))), where(~str_replace(., "Var_3_", paste0("Issue: Time"))))

I get Error in where(~str_replace(., "Var_1_", paste0("Gov't surveillance: video wave")), : could not find function "where" And I can't find it tabbing through tidyselect::

But I can run test%>% select(where(is.numeric)) %>% map(sd, na.rm = TRUE) without any issue so it does exist. What am I doing wrong?

Example data:

x <- c("_1_1",
       "_1_2",
       "_1_3",
       "_2_1",
       "_2_2",
       "_2_3",
       "_3_1",
       "_3_2",
       "_3_3",
       "_4_3")
paste0("Var",x)

test <- t(as_tibble(rnorm(10, 5.5, .35)))
colnames(test) <- paste0("Var",x)


Read more here: https://stackoverflow.com/questions/66324365/tidyselectwhere-inconsistencies-where-is-where

Content Attribution

This content was originally published by ibm at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: