Pergunta

I use R to analyze stock data stored in SQLite database. Currently the stock data is daily and stacked like this:

Code,Date,Price    
A,2013-05-01,100    
A,2013-05-02,102    
A,2013-05-03,101    
...    
B,2013-05-01,53    
B,2013-05-02,55    
B,2013-05-03,56    
...    
C,2013-05,02,56    
C,2013-05-03,51    
...

I need to transform the stacked data to to like this:

Date,A,B,C,...
2013-05-01,100,53,NULL,...
2013-05-02,102,55,56,...
2013-05-03,101,56,51,...

Is there a good way to transform the data by SQL or in R?

Foi útil?

Solução

Here are two options in R, one with base R and one with the "reshape2" package. Assuming you have read your data into an R data.frame called "mydf":

reshape(mydf, idvar="Date", timevar="Code", direction = "wide")
#         Date Price.A Price.B Price.C
# 1 2013-05-01     100      53      NA
# 2 2013-05-02     102      55      56
# 3 2013-05-03     101      56      51

library(reshape2)
dcast(mydf, Date ~ Code, value.var="Price")
#         Date   A  B  C
# 1 2013-05-01 100 53 NA
# 2 2013-05-02 102 55 56
# 3 2013-05-03 101 56 51

Outras dicas

This creates a zoo time series object with one column per stock:

library(RMySQL)
library(zoo)

# read table from database    
con <- dbConnect(MySQL(), ...whatever...)
DF <- dbGetQuery(con, "select * from stocks")

# reshape it and create a multivariate time series from it
z <- read.zoo(DF, split = 1, index = 2)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top