我读入了多个 .csv,现在想循环更改所有列名。我只能找出如何更改单个表的名称:
colnames(w01_10temp) <- c("date", "time", "temp", "na")
我还需要 R 中的 .csv 文件。在我拥有之前:
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)) }
所以我尝试将 lapply 放入循环中,但没有成功:
for (all_temp in lapply(filenames,myReadTable)) {
filenames[all_temp] <- paste(filenames[all_temp],sep="")
}
最佳答案
编写一个包装函数:
myReadTable<-function(file){
read.table(file,...)->x
names(x)<-c("date","time","temp","na")
return(x)
}
然后 lapply
它在文件名向量上以获得数据帧列表(这比使用全局变量更易于管理)。
lapply(c('fileA.csv','fileB.csv','fileC.csv'),myReadTable)
关于R:更改多个表的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6519140/