Question

I have different csv files with different names. I want to make some calculations and after that I want to save the results into one csv file.

My data of two csv files have this format:

File 1:

 day                 price
 2000-12-01 00:00:00 2 
 2000-12-01 06:00:00 3 
 2000-12-01 12:00:00 NA 
 2000-12-01 18:00:00 3 

File 2:

 day                 price
 2000-12-01 00:00:00 12 
 2000-12-01 06:00:00 NA 
 2000-12-01 12:00:00 14 
 2000-12-01 18:00:00 13 

To read the files I use this:

file1 <- read.csv(path_for_file1, header=TRUE, sep=",")
file2 <- read.csv(path_for_file2, header=TRUE, sep=",")

An example of calculation process:

library(xts)
file1 <- na.locf(file1)
file2 <- na.locf(file2)

And save the results into a csv where the timestamp is the same for the csv files:

merg <- merge(x = file1, y = file2, by = "day", all = TRUE)
write.csv(merge,file='path.csv', row.names=FALSE)

To read multiple files I tried this. Any ideas how can make the process from 2 files to be for n files?

Was it helpful?

Solution

You say that your data are comma-separated, but you show them as space-separated. I'm going to assume that your data are truly comma-separated.

Rather than reading them into separate objects, it's easier to read them into a list. It's also easier to use read.zoo instead of read.csv because merging time-series is a lot easier with xts/zoo objects.

# get list of all files (change pattern to match your actual filenames)
files <- list.files(pattern="file.*csv")
# loop over each file name and read data into an xts object
xtsList <- lapply(files, function(f) {
  d <- as.xts(read.zoo(f, sep=",", header=TRUE, FUN=as.POSIXct))
  d <- align.time(d, 15*60)
  ep <- endpoints(d, "minutes", 15)
  period.apply(d, ep, mean)
})
# set the list names to the file names
names(xtsList) <- files
# merge all the file data into one object, filling in NA with na.locf
x <- do.call(merge, c(xtsList, fill=na.locf))
# write out merged data
write.zoo(x, "path.csv", sep=",")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top