API R 中每行数据帧的调用
API Call per Row of data frame in R
我在数据框中有一列由代码组成,例如 AAPL(Apple 股票)、TWTR(Twitter)等等。我正在尝试创建一个新列,其中将 return 从股票 API 数据计算出的每个代码的股票数量。
但是当我 运行 下面的代码时,新列 "Stock Quantity" return 为每一行编辑了 NA。有人对此有解决方案吗?
library(Quandl)
portfolio <- data.frame(Code=c("AAPL", "TWTR", "MSFT"),
startingPeriod=c("2015-01-01", "2015-01-01", "2015-01-01"),
Investment=c("5000", "10000", "15000"),
stringsAsFactors=FALSE)
numberofStock <- function(pf) {
API <- Quandl(paste0("WIKI/", pf$Code), type = "raw",
start_date = pf$startingPeriod, end_date=Sys.Date())
pf["StockQuantity"] <- floor(pf$Investment_01 / tail(API$Open,1))
return(pf)
}
numberofStock(portfolio)
您可能希望首先使用 as.Date
将 portfolio$startingPeriod
转换为数据类型 Date
。此外,您将两个向量 portfolio$Code
和 portfolio$startingPeriod
传递给函数 Quandl()
。您可能想尝试使用 lapply()
函数来迭代这两个函数的每个值。
编辑:您还需要使用 as.numeric()
将 portfolio$Investment
转换为 numeric
。代码应如下所示:
portfolio <- data.frame(Code=c("AAPL", "TWTR", "MSFT"),
startingPeriod=as.Date(c("2015-01-01", "2015-01-01", "2015-01-01")),
Investment=as.numeric(c("5000", "10000", "15000")),
stringsAsFactors=FALSE)
numberofStock <- function(pf) {lapply(seq_along(nrow(portfolio)), function(x){
API <- Quandl(paste0("WIKI/", pf$Code[x]), type = "raw",
start_date = pf$startingPeriod[x], end_date=Sys.Date())
pf["StockQuantity"] <- floor(pf$Investment[x] / tail(API$Open,1))
return(pf)
})
}
numberofStock(portfolio)
这是一个开始。
library(dplyr)
company.initial =
portfolio %>%
mutate(Investment = as.numeric(Investment) ) %>%
group_by(Code) %>%
summarize(start_date = min(startingPeriod),
total_investment = sum(Investment) )
company__date =
company.initial %>%
group_by(Code) %>%
do(with(.,
Quandl(paste0("WIKI/", Code),
type = "raw",
start_date = start_date,
end_date = Sys.Date() ) ) )
company =
company__date %>%
group_by(Code) %>%
summarize(last_open = last(Open)) %>%
left_join(company.initial) %>%
mutate(StockQuantity = total_investment / last_open)
我在数据框中有一列由代码组成,例如 AAPL(Apple 股票)、TWTR(Twitter)等等。我正在尝试创建一个新列,其中将 return 从股票 API 数据计算出的每个代码的股票数量。
但是当我 运行 下面的代码时,新列 "Stock Quantity" return 为每一行编辑了 NA。有人对此有解决方案吗?
library(Quandl)
portfolio <- data.frame(Code=c("AAPL", "TWTR", "MSFT"),
startingPeriod=c("2015-01-01", "2015-01-01", "2015-01-01"),
Investment=c("5000", "10000", "15000"),
stringsAsFactors=FALSE)
numberofStock <- function(pf) {
API <- Quandl(paste0("WIKI/", pf$Code), type = "raw",
start_date = pf$startingPeriod, end_date=Sys.Date())
pf["StockQuantity"] <- floor(pf$Investment_01 / tail(API$Open,1))
return(pf)
}
numberofStock(portfolio)
您可能希望首先使用 as.Date
将 portfolio$startingPeriod
转换为数据类型 Date
。此外,您将两个向量 portfolio$Code
和 portfolio$startingPeriod
传递给函数 Quandl()
。您可能想尝试使用 lapply()
函数来迭代这两个函数的每个值。
编辑:您还需要使用 as.numeric()
将 portfolio$Investment
转换为 numeric
。代码应如下所示:
portfolio <- data.frame(Code=c("AAPL", "TWTR", "MSFT"),
startingPeriod=as.Date(c("2015-01-01", "2015-01-01", "2015-01-01")),
Investment=as.numeric(c("5000", "10000", "15000")),
stringsAsFactors=FALSE)
numberofStock <- function(pf) {lapply(seq_along(nrow(portfolio)), function(x){
API <- Quandl(paste0("WIKI/", pf$Code[x]), type = "raw",
start_date = pf$startingPeriod[x], end_date=Sys.Date())
pf["StockQuantity"] <- floor(pf$Investment[x] / tail(API$Open,1))
return(pf)
})
}
numberofStock(portfolio)
这是一个开始。
library(dplyr)
company.initial =
portfolio %>%
mutate(Investment = as.numeric(Investment) ) %>%
group_by(Code) %>%
summarize(start_date = min(startingPeriod),
total_investment = sum(Investment) )
company__date =
company.initial %>%
group_by(Code) %>%
do(with(.,
Quandl(paste0("WIKI/", Code),
type = "raw",
start_date = start_date,
end_date = Sys.Date() ) ) )
company =
company__date %>%
group_by(Code) %>%
summarize(last_open = last(Open)) %>%
left_join(company.initial) %>%
mutate(StockQuantity = total_investment / last_open)