在spread()中动态指定列名

时间:2016-02-18 16:05:04

标签: r tidyr

我正在尝试自动化一个简单的导入某些数据的过程,并使用tidyr包中的spread函数来制作宽格式数据。

以下是简化示例

Ticker <- c(rep("GOOG",5), rep("AAPL",5))
Prices <- rnorm(10, 95, 5)

Date <- rep(sapply(c("2015-01-01", "2015-01-02", "2015-01-03", "2015-01-04", "2015-01-05"),as.Date), 2)

exStockData <- data.frame(Ticker, Date, Prices)

在读取exStockData等数据框后,我希望能够创建如下所示的数据框

library(tidyr)
#this is the data frame I'd like to be able to create
desiredDataFrame <- spread(exStockData, Ticker, Prices)

但是,用于扩展函数的键参数的列不会总是被称为Ticker,而用于函数的value参数的列将不会总是被称为Price。列名是从导入文件的不同部分读入的。

#these vectors are removed because the way my text file is read in 
#I don't actually have these vectors
rm(Ticker, Prices, Date)

#the name of the first column (which serves as the key in 
#the spread function) of the exStockData data frame will 
#vary, and is read in from the file and stored as a one 
#element character vector
secID <- "Ticker"

#the name of the last column in the data frame 
#(which serves as the value in the spread function) 
#is stored also stored as a one element character vector
fields <- "Prices"

#I'd like to be able to dynamically specify the column 
#names using these other character vectors
givesAnError <- spread(exStockData, get(secID), get(fields))

1 个答案:

答案 0 :(得分:1)

传播函数文档的“另请参阅”部分提到了在这种情况下要使用的spread_函数。

在这种情况下,解决方案是使用:

solved <- spread_(exstockData, secID, fields)