R语言 跨列发生变化,并将两列作为输入传递给函数

vs91vp4v  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(124)

我有一些数据,我想进行一系列的计算。
数据如下所示:

col1        col2       col3     col1_Q      col2_Q      col3_Q
1  -0.5296808 -0.54167256  2.6416685 -0.5366250  1.19984571  0.19871640
2   1.9371126 -0.07313745  1.4779668  0.1037590  1.48971454  2.41751947
3   1.8017264 -0.19397778 -0.1828478  1.1360315 -1.11973525  0.54872807
4   0.7959393  0.54337963 -0.9444652  0.9525416 -0.82435175  0.85590371
5  -0.7666921 -1.22480510 -1.2418267  0.1530689  1.05904890 -0.43619405
6  -0.1647869  1.58908958  0.8987575  1.0575849  1.19266752  2.49259874
7   0.6760979  0.86774719  0.9480558  1.9573319 -0.00333835  0.09715238
8  -0.7226570 -0.78715755 -0.0221244  0.6955029  0.91267991  1.97211329
9  -0.2916513 -2.04597956 -1.0463820 -0.2448320 -0.80166229 -1.39243275
10 -0.5208863  1.13951660 -0.1240798 -0.9814012 -1.41960227 -0.63468344

在这部分代码中

data %>% 
  mutate(across(c(all_of(myColumnsToMutate)), ~lag(.x, 1), .names = "{.col}_L1")) %>% 
  mutate(across(c(all_of(myColumnsToMutate)), ~lag(.x, 2), .names = "{.col}_L2")) %>% 
  mutate(across(c(all_of(myColumnsToMutate)), ~lag(.x, 3), .names = "{.col}_L3")) %>% 
  mutate(across(cols = c(myColumnsToMutate), ~ my_function(.x = myColumnsToMutate[1:3], .y = myColumnsToMutate[4:6])))

我试图创建滞后,但我也试图传递2个变量给一个函数。如何使用glue来改变每个列对?例如col1col1_Q以及col2col2_Q
代码和数据

library(tidyverse)
data = data.frame(
  col1 = rnorm(1:10),
  col2 = rnorm(1:10),
  col3 = rnorm(1:10),
  
  col1_Q = rnorm(1:10),
  col2_Q = rnorm(1:10),
  col3_Q = rnorm(1:10)
)
myColumnsToMutate = colnames(data)
    data %>% 
  mutate(across(c(all_of(myColumnsToMutate)), ~lag(.x, 1), .names = "{.col}_L1")) %>% 
  mutate(across(c(all_of(myColumnsToMutate)), ~lag(.x, 2), .names = "{.col}_L2")) %>% 
  mutate(across(c(all_of(myColumnsToMutate)), ~lag(.x, 3), .names = "{.col}_L3")) %>% 
  mutate(across(cols = c(myColumnsToMutate), ~ my_function(.x = myColumnsToMutate[1:3], .y = myColumnsToMutate[4:6])))

my_function = function(var_1, var_2){
  var_1 * var_2
}
6uxekuva

6uxekuva1#

使用dplyr::cur_column()get,您可以执行以下操作:

library(dplyr, warn=FALSE)

set.seed(123)

data %>%
  mutate(across(all_of(myColumnsToMutate[1:3]),
    ~ my_function(.x, get(paste0(cur_column(), "_Q"))),
    .names = "{.col}.{.col}_Q"
  ))
#>           col1       col2       col3      col1_Q      col2_Q      col3_Q
#> 1  -0.56047565  1.2240818 -1.0678237  0.42646422 -0.69470698  0.25331851
#> 2  -0.23017749  0.3598138 -0.2179749 -0.29507148 -0.20791728 -0.02854676
#> 3   1.55870831  0.4007715 -1.0260044  0.89512566 -1.26539635 -0.04287046
#> 4   0.07050839  0.1106827 -0.7288912  0.87813349  2.16895597  1.36860228
#> 5   0.12928774 -0.5558411 -0.6250393  0.82158108  1.20796200 -0.22577099
#> 6   1.71506499  1.7869131 -1.6866933  0.68864025 -1.12310858  1.51647060
#> 7   0.46091621  0.4978505  0.8377870  0.55391765 -0.40288484 -1.54875280
#> 8  -1.26506123 -1.9666172  0.1533731 -0.06191171 -0.46665535  0.58461375
#> 9  -0.68685285  0.7013559 -1.1381369 -0.30596266  0.77996512  0.12385424
#> 10 -0.44566197 -0.4727914  1.2538149 -0.38047100 -0.08336907  0.21594157
#>    col1.col1_Q col2.col2_Q  col3.col3_Q
#> 1  -0.23902281 -0.85037817 -0.270499514
#> 2   0.06791881 -0.07481151  0.006222477
#> 3   1.39523981 -0.50713473  0.043985280
#> 4   0.06191578  0.24006594 -0.997562201
#> 5   0.10622036 -0.67143497  0.141115732
#> 6   1.18106279 -2.00689748 -2.557820824
#> 7   0.25530962 -0.20057641 -1.297525035
#> 8   0.07832211  0.91773242  0.089664034
#> 9   0.21015133  0.54703314 -0.140963090
#> 10  0.16956146  0.03941618  0.270750761

相关问题