问题描述
我有数百个文件,这些文件涉及按日期代码组织的.pet文件中的信息(19960101为YYYYMMDD格式).我正在尝试添加带有日期代码的列NDate:
I have several hundred files regarding information in .pet files organized by date code (19960101 is format YYYYMMDD). I'm trying to add a column, NDate with the date code:
for (pet.atual in files.pet) {
data.pet.atual <-
read.table(file = pet.atual,
header = FALSE,
sep = ",",
quote = "\"",
comment.char = ";");
data.pet.atual <- cbind(data.pet.atual, NDate= pet.atual)
}
例如,我要达到的目标是1996年1月1日的NDate = 19960101,1996年2月1日的NDate = 19960102,依此类推.还是for循环每次使用最新的pet.atual运行时都会替换NDate字段.谢谢
What i'm trying to achieve, for example, is for the 01-01-1996 NDate = 19960101, for 02-01-1996 NDate = 19960102 and so on. Still the for loop just replaces the NDate field everytime it runs with the latest pet.atual, ideas? Thanks
推荐答案
小的修改应该可以解决问题:
Small modification should do the trick:
data.pet.atual <- NULL
for (pet.atual in files.pet) {
tmp.data <-
read.table(file = pet.atual,
header = FALSE,
sep = ",",
quote = "\"",
comment.char = ";");
tmp.data <- cbind(tmp.data, NDate= pet.atual)
data.pet.atual <- rbind(data.pet.atual, tmp.data)
}
您也可以将tmp.data<-cbind(...)
替换为tmp.data$NDate <- pet.atual
这篇关于根据R中的文件名添加列字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!