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

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

How to use nested anonymous functions in purrr map2?

问题

以下是您要翻译的内容:

  1. 我想遍历一个数据框的列表,并给所有列名添加后缀。我希望后缀是数据框的名称。
  2. 我想要的结果基本上如下所示:
  3. ``` r
  4. library(tidyverse)
  5. list(
  6. mtcars %>% rename_with(~str_c(., "_", deparse(substitute(mtcars)))) %>% names(),
  7. iris %>% rename_with(~str_c(., "_", deparse(substitute(iris)))) %>% names()
  8. )
  9. #> [[1]]
  10. #> [1] "mpg_mtcars" "cyl_mtcars" "disp_mtcars" "hp_mtcars" "drat_mtcars"
  11. #> [6] "wt_mtcars" "qsec_mtcars" "vs_mtcars" "am_mtcars" "gear_mtcars"
  12. #> [11] "carb_mtcars"
  13. #>
  14. #> [[2]]
  15. #> [1] "Sepal.Length_iris" "Sepal.Width_iris" "Petal.Length_iris"
  16. #> [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)))

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. The result I want essentially looks like this:
  5. ``` r
  6. library(tidyverse)
  7. list(
  8. mtcars %&gt;% rename_with(~str_c(., &quot;_&quot;, deparse(substitute(mtcars)))) %&gt;% names(),
  9. iris %&gt;% rename_with(~str_c(., &quot;_&quot;, deparse(substitute(iris)))) %&gt;% names()
  10. )
  11. #&gt; [[1]]
  12. #&gt; [1] &quot;mpg_mtcars&quot; &quot;cyl_mtcars&quot; &quot;disp_mtcars&quot; &quot;hp_mtcars&quot; &quot;drat_mtcars&quot;
  13. #&gt; [6] &quot;wt_mtcars&quot; &quot;qsec_mtcars&quot; &quot;vs_mtcars&quot; &quot;am_mtcars&quot; &quot;gear_mtcars&quot;
  14. #&gt; [11] &quot;carb_mtcars&quot;
  15. #&gt;
  16. #&gt; [[2]]
  17. #&gt; [1] &quot;Sepal.Length_iris&quot; &quot;Sepal.Width_iris&quot; &quot;Petal.Length_iris&quot;
  18. #&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!

  1. imap(list(mtcars, iris), ~rename_with(.x, ~ str_c(., &quot;_&quot;, .y)))

答案1

得分: 3

imap() 要求元素具有名称,否则它会使用它们的索引。您可以使用 lst(),它会自动为其元素命名(或手动命名,例如 list(mtcars = mtcars, ...))。您还可以在 rename_with() 调用中使用基本 lambda 函数:

  1. library(purrr)
  2. library(stringr)
  3. library(dplyr)
  4. imap(lst(mtcars, iris), ~ rename_with(.x, \(var) str_c(var, "_", .y)))

或者,避免使用匿名函数并依赖参数转发:

  1. 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:

  1. library(purrr)
  2. library(stringr)
  3. library(dplyr)
  4. imap(lst(mtcars, iris), ~ rename_with(.x, \(var) str_c(var, &quot;_&quot;, .y)))

Alternatively, avoiding anonymous functions and relying on argument forwarding:

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

答案2

得分: 1

a variant with base R:

  1. list('mtcars', 'iris') |>
  2. Map(f = \(df_name){d <- get(df_name)
  3. setNames(d, paste0(names(d), '_', df_name))
  4. })
英文:

a variant with base R:

  1. list(&#39;mtcars&#39;, &#39;iris&#39;) |&gt;
  2. Map(f = \(df_name){d &lt;- get(df_name)
  3. setNames(d, paste0(names(d), &#39;_&#39;, df_name))
  4. })

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:

确定