如何在purrr的map2中使用嵌套匿名函数?

huangapple go评论73阅读模式
英文:

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&#39;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 %&gt;% rename_with(~str_c(., &quot;_&quot;, deparse(substitute(mtcars)))) %&gt;% names(),
  iris %&gt;% rename_with(~str_c(., &quot;_&quot;, deparse(substitute(iris)))) %&gt;% names()
)
#&gt; [[1]]
#&gt;  [1] &quot;mpg_mtcars&quot;  &quot;cyl_mtcars&quot;  &quot;disp_mtcars&quot; &quot;hp_mtcars&quot;   &quot;drat_mtcars&quot;
#&gt;  [6] &quot;wt_mtcars&quot;   &quot;qsec_mtcars&quot; &quot;vs_mtcars&quot;   &quot;am_mtcars&quot;   &quot;gear_mtcars&quot;
#&gt; [11] &quot;carb_mtcars&quot;
#&gt; 
#&gt; [[2]]
#&gt; [1] &quot;Sepal.Length_iris&quot; &quot;Sepal.Width_iris&quot;  &quot;Petal.Length_iris&quot;
#&gt; [4] &quot;Petal.Width_iris&quot;  &quot;Species_iris&quot;

<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(., &quot;_&quot;, .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, &quot;_&quot;, .y)))

Alternatively, avoiding anonymous functions and relying on argument forwarding:

imap(lst(mtcars, iris), rename_with, .cols = everything(), .fn = str_c, sep = &quot;_&quot;)

答案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(&#39;mtcars&#39;, &#39;iris&#39;) |&gt; 
  Map(f = \(df_name){d &lt;- get(df_name)
    setNames(d, paste0(names(d), &#39;_&#39;, df_name))
  })

huangapple
  • 本文由 发表于 2023年6月29日 14:24:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定