【问题标题】:Wikipedia views to the match trading days维基百科对比赛交易日的看法
【发布时间】:2015-03-13 09:49:08
【问题描述】:

我有这样的维基百科页面浏览数据

library(wikipediatrend)

views <-wp_trend(page = "European debt crisis",from = "2010-01-01",to = "2014-12-31",lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)

date        count
2010-01-01    128
2010-01-02    142

我有 S&P500 的数据

library(quantmod)

startDate = as.Date("2010-01-01")

endDate = as.Date("2014-12-31") 

getSymbols("^GSPC", src = "yahoo", from = startDate, to = endDate) 

Date           Open         High     Low          Close      Volume

2010-01-04     1116.56      1133.87  1116.56      1132.99    3991400000 
2010-01-05     1132.66      1136.63  1129.66      1136.52    2491020000 

现在我只想提取交易发生时维基百科页面的那些日子,即不包括周末和节假日以及飓风桑迪等非自然关闭的日子等。提取这些值的最简单方法是什么

【问题讨论】:

  • 你能再分享一点源代码吗?

标签: r wikipedia trading


【解决方案1】:

这是一个简单的子集(或过滤)问题:

# get the wikipedia views data
library(wikipediatrend)
views <-wp_trend(page = "European debt crisis",from = "2010-01-01",to = "2014-12-31",lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)

# get the stock trading data
library(quantmod)
startDate = as.Date("2010-01-01")
endDate = as.Date("2014-12-31") 
getSymbols("^GSPC", src = "yahoo", from = startDate, to = endDate) 

以下是如何在基础 R 中使用 [ 进行子集化:

# where are the trading dates in the stock data?
index(GSPC)

# where are the dates in the wikipedia data?
views$date

所以我们希望通过date 列对views 数据框进行子集化,以便它只包含在index(GSPS) 中找到的值:

# subset wikipedia data by stock data
# pattern is:
# table_to_subset[rule_to_subset_rows, rule_to_subset_columns]
# so to subset the wikipedia view data by the dates of the stock trading
# data we can do this:

wiki_data <- views[views$date %in% index(GSPC), ]

您也可以使用贡献的包data.tabledplyr 来执行此操作,如果您的数据非常大,这可能会更快。

【讨论】:

    【解决方案2】:

    是的,后来明白了,解决了

    gspcdf<-data.frame(date = index(GSPC), GSPC, row.names=NULL)
    
    
    CombDF<-merge(views,gspcdf, by.x='date', by.y='date')
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2010-10-12
    • 2011-04-22
    • 2021-08-12
    相关资源
    最近更新 更多