gpt4 book ai didi

How can I transform and filter several variables in a function by using the glue syntax and rlang, without typing their names?(如何使用GLUE语法和rlang来转换和过滤函数中的多个变量,而无需键入它们的名称?)

转载 作者:bug小助手 更新时间:2023-10-25 10:07:08 27 4
gpt4 key购买 nike



After having looked at the answers of previous questions (e.g. which-rlang-function-should-i-use-to-evaluate-a-glue-string-as-a-variable-name or glue-and-rlang-how-to-tunnel-data-variables-within-a-glue-string), I created a reprex where I could use the glue syntax and rlang functions in different cases (with case_when(), pivot_longer(), mutate(),...).

在查看了前面问题的答案(例如which-rlang-function-should-i-use-to-evaluate-a-glue-string-as-a-variable-name或glue-and-rlang-how-to-tunnel-data-variables-within-a-glue-string),)之后,我创建了一个reprex,在其中我可以在不同的情况下使用GLUE语法和Rlang函数(使用Case_When()、Pivot_Long()、Mariate()、...)。


Please do not hesitate to comment if you find some possible improvements to add to my code.

如果您发现有一些可能的改进要添加到我的代码中,请不要犹豫地发表评论。


My question is the following: is my solution too complicated or does it include the best practices about how to work with rlang and glue?

我的问题是:我的解决方案是太复杂了,还是包括了如何使用Rlang和GLue的最佳实践?


my_mean <- function(data,
input_var1,
output_var) {
input_var1_name_torgersen <- rlang::englue("{{ input_var1 }}.Torgersen")
input_var1_name_biscoe <- rlang::englue("{{ input_var1 }}.Biscoe")
input_var1 <- rlang::englue("{{ input_var1 }}")
output_name_torgersen <- rlang::englue("{{ output_var }}.Torgersen")
output_name_biscoe <- rlang::englue("{{ output_var }}.Biscoe")

data |>
dplyr::filter(
dplyr::case_when(
species == input_var1 ~ island == "Torgersen" | island == "Biscoe"
)
) |>
tidyr::pivot_wider(
names_from = c(species, island),
names_sep = ".",
values_from = body_mass_g
) |>
dplyr::group_by(year, sex) |>
dplyr::summarise(
"{input_var1_name_torgersen}" := dplyr::coalesce(eval(rlang::sym(input_var1_name_torgersen)), 0),
"{input_var1_name_biscoe}" := dplyr::coalesce(eval(rlang::sym(input_var1_name_biscoe)), 0),
"{output_name_torgersen}" := mean(eval(rlang::sym(input_var1_name_torgersen))),
"{output_name_biscoe}" := mean(eval(rlang::sym(input_var1_name_biscoe)))
) |>
dplyr::relocate(year, sex) |>
tidyr::pivot_longer(
cols = all_of(c(output_name_torgersen, output_name_biscoe)),
names_to = "species.stat.island",
values_to = "mean_body_mass_g_per_year_sex"
) |>
dplyr::select(-species.stat.island) |>
tidyr::pivot_longer(
cols = all_of(c(input_var1_name_torgersen, input_var1_name_biscoe)),
names_to = "species.island",
values_to = "body_mass_g"
) |>
tidyr::separate_wider_delim(
cols = species.island,
delim = ".",
names = c("species", "island")
) |>
dplyr::relocate(c(species, island, body_mass_g), .after = sex) |>
dplyr::distinct(year, sex, species, island, .keep_all = TRUE) |>
tidyr::drop_na(sex)
}
out <- palmerpenguins::penguins |>
dplyr::mutate(
species = as.character(species),
island = as.character(island)
) |>
my_mean(
input_var1 = Adelie,
output_var = Adelie.mean_bodymass
)
#> Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
#> dplyr 1.1.0.
#> ℹ Please use `reframe()` instead.
#> ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
#> always returns an ungrouped data frame and adjust accordingly.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> `summarise()` has grouped output by 'year', 'sex'. You can override using the
#> `.groups` argument.
out
#> # A tibble: 12 × 6
#> # Groups: year, sex [6]
#> year sex species island body_mass_g mean_body_mass_g_per_year_sex
#> <int> <fct> <chr> <chr> <dbl> <dbl>
#> 1 2007 female Adelie Torgersen 3800 2138.
#> 2 2007 female Adelie Biscoe 0 2138.
#> 3 2007 male Adelie Torgersen 3750 2415.
#> 4 2007 male Adelie Biscoe 0 2415.
#> 5 2008 female Adelie Torgersen 0 1656.
#> 6 2008 female Adelie Biscoe 3500 1656.
#> 7 2008 male Adelie Torgersen 0 1974.
#> 8 2008 male Adelie Biscoe 4300 1974.
#> 9 2009 female Adelie Torgersen 0 1597.
#> 10 2009 female Adelie Biscoe 3725 1597.
#> 11 2009 male Adelie Torgersen 0 1892.
#> 12 2009 male Adelie Biscoe 4725 1892.
out |> dplyr::glimpse()
#> Rows: 12
#> Columns: 6
#> Groups: year, sex [6]
#> $ year <int> 2007, 2007, 2007, 2007, 2008, 2008, 2008…
#> $ sex <fct> female, female, male, male, female, fema…
#> $ species <chr> "Adelie", "Adelie", "Adelie", "Adelie", …
#> $ island <chr> "Torgersen", "Biscoe", "Torgersen", "Bis…
#> $ body_mass_g <dbl> 3800, 0, 3750, 0, 0, 3500, 0, 4300, 0, 3…
#> $ mean_body_mass_g_per_year_sex <dbl> 2138.462, 2138.462, 2414.583, 2414.583, …

更多回答

So what is your question? How is this a question if you already have a solution?

那么你的问题是什么?如果你已经有了解决方案,这怎么会是一个问题呢?

@Onyambu see my edit

@Onyambu查看我的编辑

优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com