如何自动引用扩展列的名称-dplyr

时间:2018-08-24 12:06:31

标签: r dplyr

示例直接来自帮助:

stocks <- data.frame(
  time = as.Date('2009-01-01') + 0:9,
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4)
)
stocksm <- stocks %>% gather(stock, price, -time)
stocksm %>% spread(stock, price)

现在,我想创建使用X创建的列YZspread的调整后副本。我可以去

stocksm %>% spread(stock, price) %>% mutate_at(vars(X, Y, Z), ~. + 1)

,但是它需要手动指定名称,并且由于我想将所有这些操作锁定在一个函数中,因此我需要某种方式来传递新创建的列的名称。我也无法按列ID引用,因为列数必须是任意的。我该怎么办?

3 个答案:

答案 0 :(得分:1)

library(tidyr)
library(dplyr)

stocksm %>% 
  mutate_at(vars(stock), paste0, ".spread") %>% 
  spread(stock, price) %>% 
  mutate_at(vars(ends_with(".spread")), ~ . + 1) %>% 
  rename_at(vars(ends_with(".spread")), sub, 
            pattern = "\\.spread$", replacement = "")

#>          time           X            Y         Z
#> 1  2009-01-01  1.41134222  0.799908952 -2.547349
#> 2  2009-01-02 -0.03308714  1.510462889  2.909370
#> 3  2009-01-03  0.60722022 -1.725101030  4.697402
#> 4  2009-01-04  1.76681942 -2.538483592 -1.051576
#> 5  2009-01-05  0.05529327 -1.435663427  2.904005
#> 6  2009-01-06  2.69963414  2.364903583  3.957252
#> 7  2009-01-07  1.54475128 -4.289821346  3.043676
#> 8  2009-01-08  2.05165925  3.219704006 13.895450
#> 9  2009-01-09  0.05304517  0.008986952  5.257769
#> 10 2009-01-10  2.48281523  1.336480065  5.212230

答案 1 :(得分:1)

那呢:

stocksm %>% {
  variables <- unique(.$stock)
  spread(., stock, price) %>% mutate_at(vars(variables), ~. + 1)
}

但是说实话,我无法想象先传播一个有益的例子。

答案 2 :(得分:1)

您可以通过以下方式进行操作:

告诉mutate_at接受除time以外的所有列:

stocksm %>% 
  spread(stock, price) %>%
  mutate_at(setdiff(names(.),"time"), ~. + 1)

首先进行分组的“ Hack” mutate_all,因为它忽略了分组变量,并且您的操作与组无关:

stocksm %>% 
  spread(stock, price) %>% 
  group_by(time) %>%
  mutate_all(~. + 1) %>% 
  ungroup

输出

# A tibble: 10 x 4
         time      X      Y      Z
       <date>  <dbl>  <dbl>  <dbl>
 1 2009-01-01  0.180  0.910  0.775
 2 2009-01-02  1.487  0.968  0.377
 3 2009-01-03  1.738  2.888 -4.883
 4 2009-01-04  1.576  2.642 -0.913
 5 2009-01-05  0.695  2.188  2.672
 6 2009-01-06  2.512  2.838  6.435
 7 2009-01-07  1.390  2.564  0.589
 8 2009-01-08  0.379  1.149  2.551
 9 2009-01-09 -1.215 -2.979  0.785
10 2009-01-10  2.125  2.240 -4.508