R: How to change data in a column across multiple files. Help understanding lapply

坚强是说给别人听的谎言 提交于 2019-12-25 08:27:15

问题


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)
})

回答1:


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")


来源:https://stackoverflow.com/questions/37382268/r-how-to-change-data-in-a-column-across-multiple-files-help-understanding-lapp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!