I have 3 files with 3 variables: date, ID, and price. I would like to merge them by date, so if one my current files is:
date ID Price
01/01/10 A 1
01/02/10 A 1.02
01/02/10 A 0.99
...
...
I would like to get a merged file that looks like the one below for IDs A,B and C (Pr for Price):
date Pr.A Pr.B Pr.C
01/01/10 1 NA NA
01/02/10 1.02 1.2 NA
01/03/10 0.99 1.3 1
01/04/10 NA 1.23 2
01/05/10 NA NA 3
Notice that for some dates there are not prices so in that case is an NA.
My current approach works but I feel is a bit clumsy.
setwd('~where you put the files')
library(plyr)
listnames = list.files(pattern='.csv')
pp1 = ldply(listnames,read.csv,header=T) #put all the files in a data.frame
names(pp1)=c('date','ID','price')
pp1$date = as.Date(pp1$date,format='%m/%d/%Y')
# Reshape data frame so it gets organized by date
pp1=reshape(pp1,timevar='ID',idvar='date',direction='wide')
Is there any better approach you could think of?