英文:
How to use nested anonymous functions in purrr map2?
问题
以下是您要翻译的内容:
我想遍历一个数据框的列表,并给所有列名添加后缀。我希望后缀是数据框的名称。
我想要的结果基本上如下所示:
``` r
library(tidyverse)
list(
mtcars %>% rename_with(~str_c(., "_", deparse(substitute(mtcars)))) %>% names(),
iris %>% rename_with(~str_c(., "_", deparse(substitute(iris)))) %>% names()
)
#> [[1]]
#> [1] "mpg_mtcars" "cyl_mtcars" "disp_mtcars" "hp_mtcars" "drat_mtcars"
#> [6] "wt_mtcars" "qsec_mtcars" "vs_mtcars" "am_mtcars" "gear_mtcars"
#> [11] "carb_mtcars"
#>
#> [[2]]
#> [1] "Sepal.Length_iris" "Sepal.Width_iris" "Petal.Length_iris"
#> [4] "Petal.Width_iris" "Species_iris"
创建于2023年06月28日,使用reprex v2.0.2
我尝试过这样做,但没有成功。我认为嵌套的匿名函数让我感到困惑。我不确定如何引用.x(数据框)、.y(数据框名称)和列名。请帮忙!
imap(list(mtcars, iris), ~rename_with(.x, ~ str_c(., "_", .y)))
<details>
<summary>英文:</summary>
I'm looking to iterate over a list of dataframes and add a suffix to all of the column names. I want the suffix to be the name of the dataframe.
The result I want essentially looks like this:
``` r
library(tidyverse)
list(
mtcars %>% rename_with(~str_c(., "_", deparse(substitute(mtcars)))) %>% names(),
iris %>% rename_with(~str_c(., "_", deparse(substitute(iris)))) %>% names()
)
#> [[1]]
#> [1] "mpg_mtcars" "cyl_mtcars" "disp_mtcars" "hp_mtcars" "drat_mtcars"
#> [6] "wt_mtcars" "qsec_mtcars" "vs_mtcars" "am_mtcars" "gear_mtcars"
#> [11] "carb_mtcars"
#>
#> [[2]]
#> [1] "Sepal.Length_iris" "Sepal.Width_iris" "Petal.Length_iris"
#> [4] "Petal.Width_iris" "Species_iris"
<sup>Created on 2023-06-28 with reprex v2.0.2</sup>
I tried doing this but it didn't work. I think the nested anonymous functions are making me confused. I'm not sure how to reference .x (the dataframe), .y (the dataframe name), and the column name. Please help!
imap(list(mtcars, iris), ~rename_with(.x, ~ str_c(., "_", .y)))
答案1
得分: 3
imap()
要求元素具有名称,否则它会使用它们的索引。您可以使用 lst()
,它会自动为其元素命名(或手动命名,例如 list(mtcars = mtcars, ...)
)。您还可以在 rename_with()
调用中使用基本 lambda 函数:
library(purrr)
library(stringr)
library(dplyr)
imap(lst(mtcars, iris), ~ rename_with(.x, \(var) str_c(var, "_", .y)))
或者,避免使用匿名函数并依赖参数转发:
imap(lst(mtcars, iris), rename_with, .cols = everything(), .fn = str_c, sep = "_")
英文:
imap()
requires the elements to have names else it uses their index. You can use lst()
which automatically names its elements (or manually name things, e.g. list(mtcars = mtcars, ...)
. You can also use a base lambda function in the rename_with()
call:
library(purrr)
library(stringr)
library(dplyr)
imap(lst(mtcars, iris), ~ rename_with(.x, \(var) str_c(var, "_", .y)))
Alternatively, avoiding anonymous functions and relying on argument forwarding:
imap(lst(mtcars, iris), rename_with, .cols = everything(), .fn = str_c, sep = "_")
答案2
得分: 1
a variant with base R:
list('mtcars', 'iris') |>
Map(f = \(df_name){d <- get(df_name)
setNames(d, paste0(names(d), '_', df_name))
})
英文:
a variant with base R:
list('mtcars', 'iris') |>
Map(f = \(df_name){d <- get(df_name)
setNames(d, paste0(names(d), '_', df_name))
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论