所以我有一个包含多个数据集的列表。它们每个都有一个名为Index的列,其值为NA。 现在,我需要知道的是如何遍历列表或创建一个函数,该函数为每个Index-column分配特定数据集的名称?
我到目前为止想要做的是:
ProductionIowa = read.csv("../Data/Production/ProductionIowa.csv")
ProductionIllinois = read.csv("../Data/Production/ProductionIllinois.csv")
ProductionNebraska = read.csv("../Data/Production/ProductionNebraska.csv")
# preparing production data
keepList = c("Year", "County", "County.ANSI", "Value")
ProductionIowa = ProductionIowa %>%
select(keepList)
ProductionIllinois = ProductionIllinois %>%
select(keepList)
ProductionNebraska = ProductionNebraska %>%
select(keepList)
setwd("../Data/CountiesIowa/")
filenames <- gsub("\\.csv$","", list.files(pattern="\\.csv$"))
for(i in filenames){
assign(i, read.csv(paste(i, ".csv", sep="")))
}
dfs <- Filter(function(x) is(x, "data.frame"), mget(ls()))
dfs = dfs[-c(81,82,83)]
names = str_to_upper(str_sub(filenames,0,-7))
res = lapply(dfs, transform, Index = NA)
names = list(names)
非常沮丧,感谢任何帮助。
答案 0 :(得分:0)
如果要替换数据集名称中的Index列,可以使用for
循环来完成:
name_dataset = list.files(path= "../Data/Production",pattern = ".csv")
setwd("../Data/Production")
for(i in 1:length(name_dataset)
{
data = read.table(name_dataset[i],header = T)
data$Index = name_dataset[i]
write(data, name_dataset[i], sep = "\t")
}
是您要找的东西吗?