如何检索多个品牌的库存数据?

问题描述 投票:0回答:0

我正在为我的论文收集数据。在我的数据集中,我有:

  • 公司名称(大约 90 家公司)
  • 公司推文
  • 推文日期
  • 公司股票代号

在单独的列中,我想添加该特定日期的股票价格以进行事件研究——但是,我在网上找到的所有材料都只关注一家公司。作为第一步,我想收集这些公司在这段时间(即 2 年)内的所有股票数据,然后连接这两个数据集。我用过代码:

stock_data <- getSymbols( tweet_stock_merge$`Stock Symbol`, src= "yahoo", from = "2019-12-26", to = "2022-01-06")

这为我提供了股票价格,但是,每个股票都有几个数据集——有没有一种方法可以检索一个数据集中的数据?

提前非常感谢,我希望清楚我打算做什么,并且总的来说这是有道理的 - 也可能是我走错了路。

代码后:stock_data <- getSymbols( tweet_stock_merge$

Stock Symbol
, src= "yahoo", from = "2019-12-26", to = "2022-01-06")

我在网上看了看(包括ChatGPT和RTutor),我想出了以下代码:

 # Create a new column in the df data frame
df$stock_price <- NA

# Create a loop to iterate through the date levels
for (date in levels(df$date)) {
  
  # Create a subset of the df data frame for each date level
  df_date <- subset(df, date == date)
  
  # Create a loop to iterate through the stock symbols
  for (stock_symbol in levels(df_date$stock_symbol)) {
    
    # Get the stock price for the stock symbol in the date level
    stock_price <- get_stock_price(date, stock_symbol)
    
    # Update the stock_price column in the df data frame
    df$stock_price[df$date == date & df$stock_symbol == stock_symbol] <- stock_price
  }

然而,它并没有给我任何真正的结果。

r finance stock
© www.soinside.com 2019 - 2024. All rights reserved.