R 将函数应用于xts quantmod的子集

R 将函数应用于xts quantmod的子集,r,dplyr,subset,quantmod,R,Dplyr,Subset,Quantmod,我试图得到股票价格每年的标准差,但我每年都得到相同的值 我尝试使用dplyr group_by、summary和一个函数,但没有任何一个函数,它们都返回相同的值67.0 它可能正在传递整个数据帧而没有对其进行子集划分,如何解决此问题 library(quantmod) library(tidyr) library(dplyr) #initial parameters initialDate = as.Date('2010-01-01') finalDate = Sys.Date() ybeg

我试图得到股票价格每年的标准差,但我每年都得到相同的值

我尝试使用dplyr group_by、summary和一个函数,但没有任何一个函数,它们都返回相同的值67.0

它可能正在传递整个数据帧而没有对其进行子集划分,如何解决此问题

library(quantmod)
library(tidyr)
library(dplyr)

#initial parameters
initialDate = as.Date('2010-01-01')
finalDate = Sys.Date()

ybeg = format(initialDate,"%Y")
yend = format(finalDate,"%Y")

ticker = "AAPL"

#getting stock prices
stock = getSymbols.yahoo(ticker, from=initialDate, auto.assign = FALSE)
stock = stock[,4] #working only with closing prices
使用dplyr:

#Attempt 1 with dplyr - not working, all values by year return the same

stock = stock %>% zoo::fortify.zoo()
stock$Date = stock$Index
separate(stock, Date, c("year","month","day"), sep="-") %>% 
   group_by(year) %>%
   summarise(stdev= sd(stock[,2]))

# A tibble: 11 x 2
#   year  stdev
#   <chr> <dbl>
# 1 2010   67.0
# 2 2011   67.0
#....
#10 2019   67.0
#11 2020   67.0
我不知道dplyr,但下面是如何使用data.table

我不知道dplyr,但下面是如何使用data.table


不要在Summary函数中再次传递数据帧的名称。改用变量名

separate(stock, Date, c("year","month","day"), sep="-") %>% 
  group_by(year) %>% 
  summarise(stdev = sd(AAPL.Close)) # <-- here
# A tibble: 11 x 2
#   year  stdev
#   <chr> <dbl>
# 1 2010   5.37
# 2 2011   3.70
# 3 2012   9.57
# 4 2013   6.41
# 5 2014  13.4 
# 6 2015   7.68
# 7 2016   7.64
# 8 2017  14.6 
# 9 2018  20.6 
#10 2019  34.5 
#11 2020  28.7 

不要在Summary函数中再次传递数据帧的名称。改用变量名

separate(stock, Date, c("year","month","day"), sep="-") %>% 
  group_by(year) %>% 
  summarise(stdev = sd(AAPL.Close)) # <-- here
# A tibble: 11 x 2
#   year  stdev
#   <chr> <dbl>
# 1 2010   5.37
# 2 2011   3.70
# 3 2012   9.57
# 4 2013   6.41
# 5 2014  13.4 
# 6 2015   7.68
# 7 2016   7.64
# 8 2017  14.6 
# 9 2018  20.6 
#10 2019  34.5 
#11 2020  28.7 
使用apply.yearly作为更一般的period.apply的方便包装,在getSymbols返回的xts对象的年度子集上调用函数

可以使用Cl函数从getSymbols返回的对象中提取close列

stock=getSymbolsAAPL,from=2010-01-01,auto.assign=FALSE apply.yearlyClstock,sd AAPL.关闭 2010-12-31 5.365208 2011-12-30 3.703407 2012-12-31 9.568127 2013-12-31 6.412542 2014-12-31 13.371293 2015-12-31 7.683550 2016-12-30 7.640743 2017-12-29 14.621191 2018-12-31 20.593861 2019-12-31 34.538978 2020-06-19 29.577157 使用apply.yearly作为更一般的period.apply的方便包装,在getSymbols返回的xts对象的年度子集上调用函数

可以使用Cl函数从getSymbols返回的对象中提取close列

stock=getSymbolsAAPL,from=2010-01-01,auto.assign=FALSE apply.yearlyClstock,sd AAPL.关闭 2010-12-31 5.365208 2011-12-30 3.703407 2012-12-31 9.568127 2013-12-31 6.412542 2014-12-31 13.371293 2015-12-31 7.683550 2016-12-30 7.640743 2017-12-29 14.621191 2018-12-31 20.593861 2019-12-31 34.538978 2020-06-19 29.577157
谢谢@Edward,我将数据帧作为stock[2]传递,以便在处理多个ticker和调用for循环时实现自动化。我可能会将列AAPL.Close重命名为泛型,这样就可以对每个ticker通用调用它。@PedroCampos您可以使用Cl从getSymbols返回的任何ticker获取Close列。您还可以使用apply.yearly调用年度子集上的函数。谢谢@Edward,我将数据帧作为stock[,2]传递,以便在使用多个ticker和调用for循环时实现自动化。我可能会将列AAPL.Close重命名为泛型,这样就可以对每个ticker通用调用它。@PedroCampos您可以使用Cl从getSymbols返回的任何ticker获取Close列。您还可以使用apply.yearly调用年度子集上的函数。
separate(stock, Date, c("year","month","day"), sep="-") %>% 
  group_by(year) %>% 
  summarise(stdev = sd(AAPL.Close)) # <-- here
# A tibble: 11 x 2
#   year  stdev
#   <chr> <dbl>
# 1 2010   5.37
# 2 2011   3.70
# 3 2012   9.57
# 4 2013   6.41
# 5 2014  13.4 
# 6 2015   7.68
# 7 2016   7.64
# 8 2017  14.6 
# 9 2018  20.6 
#10 2019  34.5 
#11 2020  28.7