Hi RStudio Community! I have a question about updating some dplyr
code to use the current across()
function.
I have the following list:
library(dplyr)
library(purrr)
toy_lst <- list(
tbl_a = structure(list(
variable = c("text_value_a", "text_value_a"),
rownumber = 1:2,
base = c("Issue unresolved", "Issue unresolved"),
compare = c("Issue resolved", "Issue resolved")),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -2L), message = ""),
tbl_b = structure(list(
variable = "text_value_b",
rownumber = 4L,
base = "Joint pain",
compare = "Joint pain, stiffness and swelling"),
class = c("tbl_df","tbl", "data.frame"),
row.names = c(NA, -1L), message = ""),
tbl_update = structure(list(
variable = c("updated_date","updated_date", "updated_date",
"updated_date", "updated_date"),
rownumber = 1:5,
base = structure(c(18899, 18903, 18872, 18903, 18890), class = "Date"),
compare = structure(c(18903,18958, 18920, 18913, 18914), class = "Date")),
class = c("tbl_df","tbl", "data.frame"),
row.names = c(NA, -5L), message = ""),
tbl_entdate = structure(list(
variable = c("entered_date", "entered_date", "entered_date",
"entered_date", "entered_date"),
rownumber = 1:5,
base = structure(c(18899, 18929, 18857,18903, 18920), class = "Date"),
compare = structure(c(18961, 18961, 18952, 18942, 18947), class = "Date")),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -5L), message = "")
)
# view toy list
toy_lst
#> $tbl_a
#> # A tibble: 2 × 4
#> variable rownumber base compare
#> <chr> <int> <chr> <chr>
#> 1 text_value_a 1 Issue unresolved Issue resolved
#> 2 text_value_a 2 Issue unresolved Issue resolved
#>
#> $tbl_b
#> # A tibble: 1 × 4
#> variable rownumber base compare
#> <chr> <int> <chr> <chr>
#> 1 text_value_b 4 Joint pain Joint pain, stiffness and swelling
#>
#> $tbl_update
#> # A tibble: 5 × 4
#> variable rownumber base compare
#> <chr> <int> <date> <date>
#> 1 updated_date 1 2021-09-29 2021-10-03
#> 2 updated_date 2 2021-10-03 2021-11-27
#> 3 updated_date 3 2021-09-02 2021-10-20
#> 4 updated_date 4 2021-10-03 2021-10-13
#> 5 updated_date 5 2021-09-20 2021-10-14
#>
#> $tbl_entdate
#> # A tibble: 5 × 4
#> variable rownumber base compare
#> <chr> <int> <date> <date>
#> 1 entered_date 1 2021-09-29 2021-11-30
#> 2 entered_date 2 2021-10-29 2021-11-30
#> 3 entered_date 3 2021-08-18 2021-11-21
#> 4 entered_date 4 2021-10-03 2021-11-11
#> 5 entered_date 5 2021-10-20 2021-11-16
Some of these datasets have numerical/date columns, but I want to convert them all to character. I've been able to do this in the past with map(.x = toy_lst, .f = mutate_all, as.character)
:
# this convert all to character
map(.x = toy_lst, .f = mutate_all, as.character)
#> $tbl_a
#> # A tibble: 2 × 4
#> variable rownumber base compare
#> <chr> <chr> <chr> <chr>
#> 1 text_value_a 1 Issue unresolved Issue resolved
#> 2 text_value_a 2 Issue unresolved Issue resolved
#>
#> $tbl_b
#> # A tibble: 1 × 4
#> variable rownumber base compare
#> <chr> <chr> <chr> <chr>
#> 1 text_value_b 4 Joint pain Joint pain, stiffness and swelling
#>
#> $tbl_update
#> # A tibble: 5 × 4
#> variable rownumber base compare
#> <chr> <chr> <chr> <chr>
#> 1 updated_date 1 2021-09-29 2021-10-03
#> 2 updated_date 2 2021-10-03 2021-11-27
#> 3 updated_date 3 2021-09-02 2021-10-20
#> 4 updated_date 4 2021-10-03 2021-10-13
#> 5 updated_date 5 2021-09-20 2021-10-14
#>
#> $tbl_entdate
#> # A tibble: 5 × 4
#> variable rownumber base compare
#> <chr> <chr> <chr> <chr>
#> 1 entered_date 1 2021-09-29 2021-11-30
#> 2 entered_date 2 2021-10-29 2021-11-30
#> 3 entered_date 3 2021-08-18 2021-11-21
#> 4 entered_date 4 2021-10-03 2021-11-11
#> 5 entered_date 5 2021-10-20 2021-11-16
But now I want to update this code using across()
.
How can I combine mutate()
+ across()
+ as.character()
? These are my (failed) attempts:
# as another argument?
map(.x = toy_lst, .f = mutate, across, as.character)
#> Error in `.f()`:
#> ! Problem while computing `..1 = across`.
#> ✖ `..1` must be a vector, not a function.
# as a list?
map(.x = toy_lst, .f = mutate, list(across, as.character))
#> Error in `.f()`:
#> ! Problem while computing `..1 = list(across, as.character)`.
#> ✖ `..1` must be size 1, not 2.
# maybe in the reverse order?
map(.x = toy_lst, .f = mutate, list(as.character, across))
#> Error in `.f()`:
#> ! Problem while computing `..1 = list(as.character, across)`.
#> ✖ `..1` must be size 1, not 2.
Thank you in advance for your help and expertise!
Created on 2022-07-01 by the reprex package (v2.0.1)