用Repard2和Rlang突变柱

发布于 2025-01-30 17:52:50 字数 533 浏览 3 评论 0原文

我正在尝试以下操作:

library(tidyverse)
library(rlang)

df <- data.frame(a = 1:2)

reduce2(list(df, df, df), letters[2:3], ~ mutate(.x, !!(.y) := 2:3))
#> Error in local_error_context(dots = dots, .index = i, mask = mask): promise already under evaluation: recursive default argument reference or earlier problems?

我确实知道将列突变为数据框的许多方法,但是我正在尝试学习rlang

预期输出:

  a b c
1 1 2 2
2 2 3 3

I am trying the following:

library(tidyverse)
library(rlang)

df <- data.frame(a = 1:2)

reduce2(list(df, df, df), letters[2:3], ~ mutate(.x, !!(.y) := 2:3))
#> Error in local_error_context(dots = dots, .index = i, mask = mask): promise already under evaluation: recursive default argument reference or earlier problems?

I do know many ways of mutating columns to a dataframe, but I am trying to learn rlang.

The expected output:

  a b c
1 1 2 2
2 2 3 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

っ〆星空下的拥抱 2025-02-06 17:52:50

一种将purrr :: redive()rlang IS组合的方法

library(dplyr)
library(purrr)

reduce(letters[2:3], ~ .x %>% mutate(!!.y := 2:3), .init = df)

#   a b c
# 1 1 2 2
# 2 2 3 3

: 。

A method to combine purrr::reduce() and rlang is:

library(dplyr)
library(purrr)

reduce(letters[2:3], ~ .x %>% mutate(!!.y := 2:3), .init = df)

#   a b c
# 1 1 2 2
# 2 2 3 3

where the trick is to assign df to the argument .init.

独自←快乐 2025-02-06 17:52:50

我认为redain 2在这里不是正确的功能,因为您实际上并未使用任何项目,即第一次迭代后的数据帧列表。传递给redy2的函数采用三个参数 - 第一个是要减少的对象,第二个是.x .x中的下一个项目,第三个是下一个项目.y

这意味着您仍然可以使用REDAL2,请注意:

reduce2(.x = list(df, df, df), .y = letters[2:3], 
        .f = function(A, B, C) mutate(A, {{C}} := 2:3))
#>   a b c
#> 1 1 2 2
#> 2 2 3 3

但请注意,您没有在功能主体中使用第二个参数。您只需使用降低

reduce(list(df, 'b', 'c'), ~ mutate(.x, !!.y := 2:3))

I don't think reduce2 is the correct function here, since you aren't actually using any items an the list of data frames after the first iteration. The function that is passed to reduce2 takes three arguments - the first is the object being reduced, the second is the next item in .x and the third being the next item in .y.

That means you can still use reduce2 if you want, by doing:

reduce2(.x = list(df, df, df), .y = letters[2:3], 
        .f = function(A, B, C) mutate(A, {{C}} := 2:3))
#>   a b c
#> 1 1 2 2
#> 2 2 3 3

But note that you are not using the second argument in the function body. You could do it just with reduce:

reduce(list(df, 'b', 'c'), ~ mutate(.x, !!.y := 2:3))
零崎曲识 2025-02-06 17:52:50

我确定您知道您可以做df [letters [2:3]]&lt; -2:3实现相同的输出。

要使用purrrrlang您可以使用 -

library(dplyr)
library(purrr)

bind_cols(df, map_dfc(letters[2:3], ~df %>% transmute(!!.x := 2:3)))

#  a b c
#1 1 2 2
#2 2 3 3

另一种方法是 -

map(letters[2:3], ~df %>% mutate(!!.x := 2:3)) %>% reduce(inner_join, by = 'a')

I am sure you are aware that you can do df[letters[2:3]] <- 2:3 to achieve the same output but I don't think this is what you are looking for.

To use purrr and rlang you may use -

library(dplyr)
library(purrr)

bind_cols(df, map_dfc(letters[2:3], ~df %>% transmute(!!.x := 2:3)))

#  a b c
#1 1 2 2
#2 2 3 3

And another way would be -

map(letters[2:3], ~df %>% mutate(!!.x := 2:3)) %>% reduce(inner_join, by = 'a')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文