Homunculus Reticulli
Homunculus Reticulli

Reputation: 68386

zoo merge() and merged column names

I am relatively new to R. I am merging data contained in multiple csv files into a single zoo object.

Here is a snippet of the code in my for loop:

temp <- read.csv(filename, stringsAsFactors=F)
temp_dates <- as.Date(temp[,2])
temp <- zoo(temp[,17], temp_dates)
dataset <- temp[seq_specified_dates]

# merge data into output
if (length(output) == 0)
    output <- dataset
else
    output <- merge(output, dataset, all=FALSE)

When I run head() on the output zoo object, I notice bizarrely named column names like: 'dataset.output.output.output' etc. How can I assign more meaningful names to the merged columns. ?

Also, how do I reference a particular column in a zoo object?. For example if output was a dataframe, I could reference the 'Patient_A' column as output$Patient_A. How do I reference a specific column in a merged zoo object?

Upvotes: 2

Views: 1318

Answers (2)

Oscar Perpi&#241;&#225;n
Oscar Perpi&#241;&#225;n

Reputation: 4511

read.zoo is able to read and merge multiple files. For example:

idx <- seq(as.Date('2012-01-01'),  by = 'day',  length = 30)

dat1<- data.frame(date = idx,  x = rnorm(30))
dat2<- data.frame(date = idx,  x = rnorm(30))
dat3<- data.frame(date = idx,  x = rnorm(30))

write.table(dat1,  file = 'ex1.csv')
write.table(dat2,  file = 'ex2.csv')
write.table(dat3,  file = 'ex3.csv')

datMerged <- read.zoo(c('ex1.csv',  'ex2.csv',  'ex3.csv'))

If you want to access a particular column you can use the $ method:

datMerged$ex1.csv

EDITED: You can extract a time period with the window method:

window(datMerged, start='2012-01-28', end='2012-01-30')

The xts package includes more extraction methods:

library(xts)
datMergedx['2012-01-03']
datMergedx['2012-01-28/2012-01-30']

Upvotes: 2

aatrujillob
aatrujillob

Reputation: 4816

I think this would work regardless of the date being a zoo class, if you provide an example I may be able to fix the details, but all in all this should be a good starting point.

#1- Put your multiple csv files in one folder
setwd(your path)
listnames = list.files(pattern=".csv")

#2-use package plyr
library(plyr)

    pp1 = ldply(listnames,read.csv,header=T) #put all the files in on data.frame
    names(pp1)=c('name1','name2','name3',...)
    pp1$date =  zoo(pp1$date)


# Reshape data frame so it gets organized by date
pp2=reshape(pp1,timevar='name1',idvar='date',direction='wide')

Upvotes: 2

Related Questions