You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to recode certain columns based on values of columns that have a similar naming. I'm using 1. case_when() to recode, 2. cur_column() to programmatically select the "related" columns, and 3. across() to automatically do this across the relevant columns.
As shown below, I can do this when specifying one column, but not with cur_column(). It complains that it must be used within across(), even though it is being used within across()
library(dplyr)
#> #> Attaching package: 'dplyr'#> The following objects are masked from 'package:stats':#> #> filter, lag#> The following objects are masked from 'package:base':#> #> intersect, setdiff, setequal, union
library(stringr)
names_list<-
c("x", "y", "color")
## worksstarwars|>
rename(color=hair_color) |>
mutate(across(
skin_color,
\(x)
case_when(
x=="fair"~x,
is.na(!!sym(na.omit(str_extract("skin_color", names_list)))) ~NA,
.default="other"
)
)) |>
select(color, skin_color)
#> # A tibble: 87 × 2#> color skin_color#> <chr> <chr> #> 1 blond fair #> 2 <NA> <NA> #> 3 <NA> <NA> #> 4 none other #> 5 brown other #> 6 brown, grey other #> 7 brown other #> 8 <NA> <NA> #> 9 black other #> 10 auburn, white fair #> # ℹ 77 more rows## doesn't workstarwars|>
rename(color=hair_color) |>
mutate(across(
skin_color,
\(x)
case_when(
x=="fair"~x,
is.na(!!sym(na.omit(str_extract(cur_column(), names_list)))) ~NA,
.default="other"
)
)) |>
select(color, skin_color)
#> Error in `cur_column()`:#> ! Must only be used inside `across()`.
The !! forces cur_column() to be evaluated immediately, even before the mutate() call. This is a known limitation that we don't have a great solution for right now, but this feels like a somewhat niche case and what @nirguk suggests seems like it would be a bit cleaner anyways
I want to recode certain columns based on values of columns that have a similar naming. I'm using 1.
case_when()
to recode, 2.cur_column()
to programmatically select the "related" columns, and 3.across()
to automatically do this across the relevant columns.As shown below, I can do this when specifying one column, but not with
cur_column()
. It complains that it must be used withinacross()
, even though it is being used withinacross()
Created on 2024-01-15 with reprex v2.1.0
Session info
The text was updated successfully, but these errors were encountered: