0

I have a folder with about 160 files that are formatted with three columns: onset time, variable1 'x', and variable 2 'y'. Onset is listed in R as a string, but it is a time variable which is Hour:Minute:Second:FractionalSecond. I need to remove the fractional second. If I could round that would be great, but it would be okay to just remove the fractional second using something like substr(file$onset,1,8).

My files are named in a format similar to File001 File002 File054 File1001

onset   X   Y
00:55:17:95 3   3
00:55:29:66 3   4
00:55:31:43 3   3
01:00:49:24 3   3
01:02:00:03 

I am trying to use lapply. lapply seems simple, but I'm having a hard time figuring it out. The code written below returns an error that the final line doesn't have 3 elements. For my final output it is important that my last line only have the value for onset.

lapply(files, function(x) {
t <- read.table(x, header=T) # load file
t$onset<-substr(t$onset,1,8)
out <- function(t)
  # write to file
write.table(out, "filepath", sep="\t", quote=F, row.names=F, col.names=T)
})
  • Seems like your error is read.table issue, i.e one of the line in one of the 160 files does not have 3 elements. try to see if that error is replicated when you run `lapply(files, function(x) read.table(x, header=T))` – Adam Quek May 23 '16 at 04:25

1 Answers1

0
First create a data frame of all text files, then you can apply strptime and format functions for the same vector to remove the fractional second.
filelist <- list.files(pattern = "\\.txt")
alltxt.files <- list()  # create a list to populate with table data (if you wind to bind all the rows together)
count <- 1
for (file in filelist) {
  dat <- read.table(file,header = T)
  alltxt.files[[count]] <- dat # creat a list of rows from txt files
  count <- count + 1
}
allfiles <- do.call(rbind.data.frame, alltxt.files)

allfiles$onset <- strptime(allfiles$onset,"%H:%M:%S")
allfiles$onset <- format(allfiles$onset,"%H:%M:%S")
Arun kumar mahesh
  • 2,289
  • 2
  • 14
  • 22