仅从viewFinancials获取数字输出而无需其他文本

时间:2017-04-24 15:27:18

标签: r quantmod

我用以下方式计算了微软的股息收益率:

# load financial data for MSFT
library(quantmod)
getFinancials('MSFT')

# calculate dividend yield for MSFT
as.numeric(first(-viewFinancials(MSFT.f, type='CF', period='A',subset = NULL)['Total Cash Dividends Paid',]/viewFinancials(MSFT.f, type='BS', period='A',subset = NULL)['Total Common Shares Outstanding',]))

这是输出

Annual Cash Flow Statement for MSFT
Annual Balance Sheet for MSFT
[1] 1.40958

如果没有附加文字1.40958Annual Cash Flow Statement for MSFT,如何只能使用数字输出Annual Balance Sheet for MSFT?有没有办法压制那些?

1 个答案:

答案 0 :(得分:1)

两个字符串“MSFT年度现金流量表”和“MSFT年度资产负债表”是来自viewFinancials的消息。他们没有以任何方式附加到结果。

R> dy <- as.numeric(first(-viewFinancials(MSFT.f, type='CF', period='A',subset = NULL)['Total Cash Dividends Paid',]/viewFinancials(MSFT.f, type='BS', period='A',subset = NULL)['Total Common Shares Outstanding',]))
Annual Cash Flow Statement for MSFT
Annual Balance Sheet for MSFT
R> dy
[1] 1.40958

如果您想压制消息,请使用suppressMessages()

R> suppressMessages(dy <- as.numeric(first(-viewFinancials(MSFT.f, type='CF', period='A',subset = NULL)['Total Cash Dividends Paid',]/viewFinancials(MSFT.f, type='BS', period='A',subset = NULL)['Total Common Shares Outstanding',])))
R> dy
[1] 1.40958
R>