开发者

R: change columns names of multiple tables

开发者 https://www.devze.com 2023-03-15 23:30 出处:网络
I read in multiple .csv and now want to change all their column names in a loop. I could only find out how to change the names of a single table:

I read in multiple .csv and now want to change all their column names in a loop. I could only find out how to change the names of a single table:

colnames(w01_10temp) <- c("date", "time", "temp", "na") 

I also nee .csv files in R. Before I had:

filenames <- list.files(path=getwd()) 
numfiles <- length(filenames) 
for (all_temp in c(1:numfiles)) {
filenames[all_temp] <- paste(filenames[all_temp],sep="")
assign(gsub([.]ASC$","temp",filenames[all_temp]),read.delim2(filenames[all_temp], fileEncoding="ISO-8859-15", skip = 4))    }

So I tried putting the lapply in the loop, withou开发者_开发技巧t success:

for (all_temp in lapply(filenames,myReadTable)) {
  filenames[all_temp] <- paste(filenames[all_temp],sep="")
  }


Write a wrapper function:

myReadTable<-function(file){
 read.table(file,...)->x
 names(x)<-c("date","time","temp","na")
 return(x)
}

And then lapply it over the file name vector to get a list of data frames (this is much more manageable than using global variables).

lapply(c('fileA.csv','fileB.csv','fileC.csv'),myReadTable)
0

精彩评论

暂无评论...
验证码 换一张
取 消