How to rename column with tq_mutate

114 Views Asked by At

When I use tq_transmute, I am able to rename all the columns. When I use tq_mutate, only the first 2 columns are getting renamed. The last two columns remain as it is. Is this a limitation, or am I missing something to fix for tq_mutate?

In the first output below, you will see that "TR_high","TR_Low" is not printed as the last 2 columns.

    #https://www.rpubs.com/stephenodea54/776350
# Loads tidyquant, lubridate, xts, quantmod, TTR, and PerformanceAnalytics
library(tidyverse)
library(tidyquant) 
library(ggthemes)
startdt <- "2021-02-01"
AMC <- tq_get(
  "AMC",
  get = "stock.prices",
  from = startdt
)
test1<-AMC %>% 
  tq_mutate(select = c("high", "low", "close"), n=14, mutate_fun = ATR,col_rename = c("TrueRange","ATR","TR_high","TR_Low"))  # %>%


head(test1)

# A tibble: 6 x 12
  symbol date        open  high   low close    volume adjusted TrueRange   ATR ATR..1 ATR..2
  <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>     <dbl> <dbl>  <dbl>  <dbl>
1 AMC    2021-02-01 17    17.2  12.9  13.3  434608000    13.3      NA       NA  NA     NA   
2 AMC    2021-02-02  9.48 10.1   6     7.82 462775900     7.82      7.3     NA  13.3    6   
3 AMC    2021-02-03  8.85  9.77  7.89  8.97 221405100     8.97      1.95    NA   9.77   7.82

Now, I am just using a different function, and all the 4 columns are nicely renamed.

  test2<-AMC %>% 
      tq_transmute(select = c("high", "low", "close"), n=14, mutate_fun = ATR,col_rename = c("TrueRange","ATR","TR_high","TR_Low"))  # %>%
    head(test2)

# A tibble: 6 x 5
  date       TrueRange   ATR TR_high TR_Low
  <date>         <dbl> <dbl>   <dbl>  <dbl>
1 2021-02-01     NA       NA   NA     NA   
2 2021-02-02      7.3     NA   13.3    6   
3 2021-02-03      1.95    NA    9.77   7.82
0

There are 0 best solutions below