如何使用 tidyverse 中的相邻列修改在另一列中命名的列?

发布于 2025-01-12 14:06:42 字数 825 浏览 1 评论 0原文

我有以下数据框,

df <- tibble(x = c(2, 3, 4)) %>% 
mutate(`1` = 99, `2` = 88, `3` = 77, `4` = 66, `5` = 55)

x 包含需要操作的列名称,该列中的值必须替换为列 x-1x-1 中的值之和代码>、<代码>x和<代码>x+1。例如,对于 x 为 2 的第一行,2 列中的值必须替换为 (99+88+77) = 264。

我尝试使用双卷曲括号({{}}) 和 :=,如下所示,

df %>% 
  mutate("{{x}}" := {{x-1}} + {{x}} + {{x+1}})

但出现以下错误, local_error_context(dots =dots, .index = i, mask = mask) 中的错误:promise 已在评估中:递归默认参数引用或早期问题?

然后我尝试使用 cur_column( )across() 内,如下所示,

df %>% 
  mutate(across(-x, ~if_else(x == cur_column(), {{cur_column()}}, .x)))

我得到了与上面相同的错误,我想我可能错误地使用了卷曲运算符,有人可以帮忙吗?

I've the following dataframe,

df <- tibble(x = c(2, 3, 4)) %>% 
mutate(`1` = 99, `2` = 88, `3` = 77, `4` = 66, `5` = 55)

column x holds the column names which needs to be manipulated, the value in that column has to be replaced with the sum of values in columns x-1, x and x+1. For example, for the first row where x is 2, value in column 2 has to be replaced with (99+88+77) = 264.

I tried using double curly brackets({{}}) and :=, like below,

df %>% 
  mutate("{{x}}" := {{x-1}} + {{x}} + {{x+1}})

but I'm getting the following error,
Error in local_error_context(dots = dots, .index = i, mask = mask) : promise already under evaluation: recursive default argument reference or earlier problems?

Then I tried accessing the column using cur_column() inside across() like below,

df %>% 
  mutate(across(-x, ~if_else(x == cur_column(), {{cur_column()}}, .x)))

and I'm getting the same error as above, I think I may be using the curly operator incorrectly, can someone help, please?

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

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

发布评论

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

评论(2

旧伤慢歌 2025-01-19 14:06:42

{{}} 语法适用于将未计算的表达式传递给 dplyr 命令时,它不适用于捕获列值。

大多数时候,在每行的不同列上执行不同的选项并不容易。一种替代方法是重塑数据,以便可以使用超前/滞后函数。然后你就可以向后旋转。

library(dplyr)
library(tidyr)
df %>% 
  mutate(row = row_number()) %>% 
  pivot_longer(!c(x, row)) %>% 
  mutate(name = as.integer(name)) %>% 
  group_by(row) %>% 
  mutate(value=if_else(x==name, value + lead(value) + lag(value), value)) %>% 
  pivot_wider(c(row, x)) %>% 
  select(-row)

#     row     x   `1`   `2`   `3`   `4`   `5`
#   <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     1     2    99   264    77    66    55
# 2     2     3    99    88   231    66    55
# 3     3     4    99    88    77   198    55

另一种选择是创建一个辅助函数,可以使用 rowwise() 访问 cur_column()cur_data(),以便创建不同的每行的转换。


colclump <- function(target) {
  prevc <- as.character(as.integer(target)-1)
  nextc <- as.character(as.integer(target)+1)
  function(x) {
    if (cur_column()==target) {
      x + cur_data()[[prevc]] + cur_data()[[nextc]]
    } else {
      x
    }
  }
  
}
df %>% 
  rowwise() %>% 
  mutate(across(-x, ~colclump(x)(.x)))

The {{}} syntax is for when you are passing unevaluated expressions to a dplyr command, it does not work for capturing column values.

Most of the time it's not easy to do different options on different columns for each row. One alternative is to reshape your data so you can use lead/lag functions. Then you can pivot back.

library(dplyr)
library(tidyr)
df %>% 
  mutate(row = row_number()) %>% 
  pivot_longer(!c(x, row)) %>% 
  mutate(name = as.integer(name)) %>% 
  group_by(row) %>% 
  mutate(value=if_else(x==name, value + lead(value) + lag(value), value)) %>% 
  pivot_wider(c(row, x)) %>% 
  select(-row)

#     row     x   `1`   `2`   `3`   `4`   `5`
#   <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     1     2    99   264    77    66    55
# 2     2     3    99    88   231    66    55
# 3     3     4    99    88    77   198    55

Another alternative is to create a helper function that can access the cur_column() and cur_data() with rowwise() in order to create a different transformation for each row.


colclump <- function(target) {
  prevc <- as.character(as.integer(target)-1)
  nextc <- as.character(as.integer(target)+1)
  function(x) {
    if (cur_column()==target) {
      x + cur_data()[[prevc]] + cur_data()[[nextc]]
    } else {
      x
    }
  }
  
}
df %>% 
  rowwise() %>% 
  mutate(across(-x, ~colclump(x)(.x)))
舂唻埖巳落 2025-01-19 14:06:42

也许这也符合你的目的:

for(k in 2:4) {
  df[k-1,k+1] <- df[k-1,as.character(df$x[k-1])] + 
              df[k-1,as.character(df$x[k-1]-1)] + 
              df[k-1,as.character(df$x[k-1]+1)]
}
df
#  A tibble: 3 x 6
# x   `1`   `2`   `3`   `4`   `5`
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     2    99   264    77    66    55
# 2     3    99    88   231    66    55
# 3     4    99    88    77   198    55

Maybe this one also serves your purpose:

for(k in 2:4) {
  df[k-1,k+1] <- df[k-1,as.character(df$x[k-1])] + 
              df[k-1,as.character(df$x[k-1]-1)] + 
              df[k-1,as.character(df$x[k-1]+1)]
}
df
#  A tibble: 3 x 6
# x   `1`   `2`   `3`   `4`   `5`
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     2    99   264    77    66    55
# 2     3    99    88   231    66    55
# 3     4    99    88    77   198    55
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文