0

当我使用 tq_transmute 时,我可以重命名所有列。当我使用 tq_mutate 时,只有前 2 列被重命名。最后两列保持原样。这是一个限制,还是我缺少要修复 tq_mutate 的东西?

在下面的第一个输出中,您将看到“TR_high”、“TR_Low”没有作为最后 2 列打印。

    #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

现在,我只是使用了一个不同的函数,所有 4 列都被很好地重命名了。

  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
4

0 回答 0