简体   繁体   English

如何使用 tq_transmute() 将一列添加到 tibble 中?

[英]How to add a column into a tibble using tq_transmute()?

I have the following R script, which I use to retrieve the symbols of all US equities and compute their returns over some period of time:我有以下 R 脚本,我用它来检索所有美国股票的符号并计算它们在一段时间内的回报:

library(dplyr)
library(tidyverse)
library(tidyquant)

symbols <- stockSymbols(exchange = c("AMEX", "NASDAQ", "NYSE"), sort.by = c("Exchange","Symbol"), quiet = FALSE)

symbol_array = symbols$Symbol[c(0:5)]

start_date <- as.Date("2019-03-11")
end_date <- as.Date("2020-03-13")

stock_price_data = tq_get(symbol_array, from = start_date, to = end_date, get = "stock.prices")

stock_returns_data = stock_price_data %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'yearly',
               col_rename = 'returns')

stock_returns_data

The output of this script is:这个脚本的输出是:

# A tibble: 10 x 3
# Groups:   symbol [5]
   symbol date        returns
   <chr>  <date>        <dbl>
 1 AAMC   2019-12-31 -0.645  
 2 AAMC   2020-03-12  0.415  
 3 AAU    2019-12-31 -0.0167 
 4 AAU    2020-03-12 -0.508  
 5 ACU    2019-12-31  0.436  
 6 ACU    2020-03-12 -0.108  
 7 ACY    2019-12-31 -0.633  
 8 ACY    2020-03-12 -0.298  
 9 AE     2019-12-31  0.00138
10 AE     2020-03-12 -0.371 

The stockSymbols() function returns the following: stockSymbols() 函数返回以下内容:

  Symbol                             Name LastSale MarketCap IPOyear           Sector                        Industry Exchange
1   AAMC Altisource Asset Management Corp  15.9300   $25.79M      NA          Finance                     Real Estate     AMEX
2    AAU           Almaden Minerals, Ltd.   0.2801   $31.29M    2015 Basic Industries                 Precious Metals     AMEX
3    ACU         Acme United Corporation.  21.6600   $72.61M    1988    Capital Goods Industrial Machinery/Components     AMEX
4    ACY                AeroCentury Corp.   2.8259    $4.37M      NA       Technology Diversified Commercial Services     AMEX
5     AE   Adams Resources & Energy, Inc.  23.0000   $97.42M      NA           Energy          Oil Refining/Marketing     AMEX

Question: is it possible to include the "Sector" in the output of the returns data?问题:是否可以在返回数据的输出中包含“部门”?

For example:例如:

# A tibble: 10 x 3
# Groups:   symbol [5]
   symbol date        returns  sector
   <chr>  <date>        <dbl>  <char>
 1 AAMC   2019-12-31 -0.645    Finance
 2 AAMC   2020-03-12  0.415    Finance
 3 AAU    2019-12-31 -0.0167   Basic Industries
 4 AAU    2020-03-12 -0.508    Basic Industries
 5 ACU    2019-12-31  0.436    Capital Goods
 6 ACU    2020-03-12 -0.108    Capital Goods
 7 ACY    2019-12-31 -0.633    Technology
 8 ACY    2020-03-12 -0.298    Technology
 9 AE     2019-12-31  0.00138  Energy
10 AE     2020-03-12 -0.371    Energy

Thanks in advance!提前致谢!

One option is a left_join with the 'symbols' dataset after select ing only the relevant columns from 'symbols'一种选择是在left_join “符号”中select相关列后与“符号”数据集进行left_join

library(dplyr)
left_join(stock_returns_data, symbols %>%
                                select(symbol = Symbol, Sector))

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

相关问题 如何使用 R 中 tidyquant 包中的 tq_transmute 获取 OHLC 每月高低数据 - How to get OHLC monthly high and low data using tq_transmute from tidyquant package in R 使用 tq_transmute() function 时如何保留所有列? - How do I retain all the columns while using tq_transmute() function? 带有 tq_transmute 的 tibbletime 产生奇怪的错误,列显然存在但说它不存在 - tibbletime with tq_transmute producing strange error, column obviously exists but says it doesn't Tidyquant tq_get 和 tq_transmute:警告消息“从数据中删除的缺失值” - Tidyquant tq_get and tq_transmute: Warning message "missing values removed from data" 如何在使用transmute_if / transmute_at的情况下实现由列划分的大标题 - How to achieve a large tibble divided by a column within using transmute_if / transmute_at 如何根据来自另一个 tibble 的信息添加 tibble 列 - How to add a tibble column based on information from another tibble 如何在 R 中将列添加到网状小标题 - how to add a column to a netsed tibble in R 将 transmute_at 与数据框中的 integer 列一起使用 - Using transmute_at with integer column in data frame dplyr:如何使用 count() 将列保留在 tibble 中 - dplyr: how to keep a column in a tibble using count() 如何使用 tidyverse 向 tibble 中所有组的每一列添加额外的行? - How do I add extra rows to each column across all groups in tibble using tidyverse?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM