【发布时间】:2018-12-08 09:03:42
【问题描述】:
我的文件名称带有年份。我列出了所有文件并将年份值取为变量名。我想在输出文件中创建一个包含所有 365 天的列。但是如何在日期序列中给出具有年份值的变量名称?
文件名的格式为“E1901.txt”、“E1902.txt”、.... 这是脚本
setwd("location")
input_files = list.files(,pattern="[.]txt$")
total = data.frame()
for(i in 1:length(input_files)){
rf = read.csv(input_files[i])
year = regmatches(rf,regexec("E(.+).txt",rf))
year=sapply(year,"[",2)
print(year)
filenm = sub("txt","csv",rf)
date = seq(as.Date(paste(year,"/1/1")), as.Date(paste(year,"/12/31")), "day")
rf$date = date
rf= rf[,c(220,1:219)]
}
cat("\n Finished processing data of ",filenm)
total = do.call("rbind",rf)
write.csv(total, file="1901-2016.csv", row.names=FALSE, col.names=FALSE,sep =",")
问候,
【问题讨论】:
-
欢迎来到 StackOverflow。请阅读how to ask 和how to make a great reproducible R example,以便我们为您提供充分的帮助。