简体   繁体   English

使用 tq_transmute() function 时如何保留所有列?

[英]How do I retain all the columns while using tq_transmute() function?

I am trying to replicate a trading strategy and backtest in R.我正在尝试在 R 中复制交易策略和回测。 However, I am having a slight problem with the tq_transmute() function.但是,我对 tq_transmute() function 有一点问题。 Any help would be appreciated.任何帮助,将不胜感激。

So, I have the following code that I have written until now:因此,到目前为止,我已经编写了以下代码:

       #Importing the etfs data
        symbols<- c("SPY","XLF","XLE")
        start<-as.Date("2000-01-01")
        end<- as.Date("2018-12-31")
        
        price_data<- lapply(symbols, function(symbol){
              etfs<-as.data.frame(getSymbols(symbol,src="yahoo", from=start, to= end, 
                                             auto.assign = FALSE))
              colnames(etfs)<- c("Open", "High","Low","Close","volume","Adjusted")
              etfs$Symbol<- symbol
              etfs$Date<- rownames(etfs)
              etfs
            })
    
     # Next, I used do.call() with rbind() to combine the data into a single data frame   
        
        etfs_df<- do.call(rbind, price_data)
    
        #This because of POSIXct error
        daily_price<- etfs_df %>%
                  mutate(Date=as.Date(Date, frac=1)) 
# I have deleted some columns of the table as my work only concerned the "Adjusted" column. 
#So, until now we have:
 
        head(daily_price)
    
          Adjusted Symbol       Date
        1 98.14607    SPY 2000-01-03
        2 94.30798    SPY 2000-01-04
        3 94.47669    SPY 2000-01-05
        4 92.95834    SPY 2000-01-06
        5 98.35699    SPY 2000-01-07
        6 98.69440    SPY 2000-01-10
    
        #Converting the daily adjusted price to monthly adjusted price
    
        monthly_price<- 
      tq_transmute(daily_price,select = Adjusted, mutate_fun = to.monthly, indexAt = "lastof")
    
    head(monthly_price)
    
    # And now, I get the following table: 
    
    # A tibble: 6 x 2
      Date       Adjusted
      <date>        <dbl>
    1 2000-01-31     16.6
    2 2000-02-29     15.9
    3 2000-03-31     17.9
    4 2000-04-30     17.7
    5 2000-05-31     19.7
    6 2000-06-30     18.6

So, as you can see, the Date and Adjusted prices have been successfully converted to monthly figures but my Symbol column has disappeared.因此,如您所见,日期和调整后的价格已成功转换为月度数据,但我的符号列消失了。 Could anyone please tell me why did that happen and how do I get it back?谁能告诉我为什么会发生这种情况以及如何找回它?

Thank you.谢谢你。

group the data by Symbol and apply tq_transmute .Symbol对数据进行分组并应用tq_transmute

library(dplyr)
library(quantmod)
library(tidyquant)

monthly_price <- daily_price %>%
                  group_by(Symbol) %>%
                  tq_transmute(daily_price,select = Adjusted, 
                               mutate_fun = to.monthly, indexAt = "lastof")

#  Symbol Date       Adjusted
#   <chr>  <date>        <dbl>
# 1 SPY    2000-01-31     94.2
# 2 SPY    2000-02-29     92.7
# 3 SPY    2000-03-31    102. 
# 4 SPY    2000-04-30     98.2
# 5 SPY    2000-05-31     96.6
# 6 SPY    2000-06-30     98.5
# 7 SPY    2000-07-31     97.0
# 8 SPY    2000-08-31    103. 
# 9 SPY    2000-09-30     97.6
#10 SPY    2000-10-31     97.2
# … with 674 more rows

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 tq_transmute() 将一列添加到 tibble 中? - How to add a column into a tibble using tq_transmute()? 如何使用 R 中 tidyquant 包中的 tq_transmute 获取 OHLC 每月高低数据 - How to get OHLC monthly high and low data using tq_transmute from tidyquant package in R Tidyquant tq_get 和 tq_transmute:警告消息“从数据中删除的缺失值” - Tidyquant tq_get and tq_transmute: Warning message "missing values removed from data" 带有 tq_transmute 的 tibbletime 产生奇怪的错误,列显然存在但说它不存在 - tibbletime with tq_transmute producing strange error, column obviously exists but says it doesn't 如何将transmute与grep function结合起来? - How to combine transmute with grep function? 如何在函数中传递要在tq_mutate(TidyQuant)中选择的变量列表? - How do you pass a list of variables to select in tq_mutate (TidyQuant) from within a function? 转换所有列:删除逗号和逗号后的每个字符 - transmute over all columns : removing comma and every characters after comma 如何获得保留分配的值的功能? (R) - How do I get a function to retain values I assign? (R) 使用R如何保留带有关键字的句子 - using R How do I retain a sentence with a key word 我有一个很长的数据集。 如何在使用排名功能限制创建的列数时转换为宽格式? - I have a long data set. How do I convert to wide format while using a rank function to limit the number of columns created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM