简体   繁体   English

如何将多个数据框的特定列的值更改为数据框名称本身的值?

[英]How to change the value of a specific column of multiple dataframes to the value of the dataframes' names themselves?

So I have got a list containing multiple datasets.所以我有一个包含多个数据集的列表。 Each of them have a column named Index with the value of NA.它们每个都有一个名为 Index 的列,其值为 NA。 Now what I need to know is how can I loop through the list, or create a function, which assigns to every Index-column the name of the specific dataset?现在我需要知道的是如何遍历列表,或者创建一个 function,它为每个索引列分配特定数据集的名称?

What I tried to do up to now is following:到目前为止,我尝试做的事情如下:

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) 

Very frustrated and appreciating any help, thanks.非常沮丧并感谢任何帮助,谢谢。

If you want to replace the column Index par the name of the dataset, you can do it using a for loop:如果要将列 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")
}

Is it what you are looking for?是你要找的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM