简体   繁体   English

将新列添加到数据框列表

[英]Adding new column to list of data frames

I'm cleaning up some data I'm importing from excel, from a large number of files, and have to create two keys based on subscript from the file name during the import. 我正在从大量文件中清除从excel导入的一些数据,并且在导入过程中必须根据文件名的下标创建两个键。 The files are imported by country. 文件是按国家导入的。 I managed to import the files into separate data frames, but ran into difficulties creating the new variables. 我设法将文件导入到单独的数据框中,但是在创建新变量时遇到了困难。 For brevity, I have set country="usa" and year=1980. 为简便起见,我将country =“ usa”和year = 1980设置为。

My first thought was to create a list of the data frames present in the environment, by filtering for the pattern "USA". 我的第一个想法是通过过滤模式“ USA”来创建环境中存在的数据帧的列表。 However, this is poor style, and the product seems to be a character list which is not connected to the data frames themselves. 但是,这是较差的样式,产品似乎是一个字符列表,未与数据框本身连接。

usadflist <- Filter(is.data.frame, mget(ls(pattern="USA")))

This is when I turned to adjusting the actual import, using dplyr::mutate(), however could not specify a variable that hasn't been created yet, which is when I tried cbind(), as follows: 这是我转向使用dplyr :: mutate()调整实际导入的时间,但是无法指定尚未创建的变量,也就是我尝试cbind()时,如下所示:

usalist <- list.files(path ="~/Desktop/reports/usa")

for(i in usalist) {
  assign(paste(i),read_excel(path = paste("Desktop/reports/usa/",i,sep="")))
  cbind(usalist[[i]][country]<-"usa", usalist[[i]][year]<-1980)
}

An error message is given: Error in *tmp* [[i]] : subscript out of bounds . 给出错误消息: *tmp* [[i]]中的错误:下标超出范围。

What I am expecting is that each of the data frames will have a new variable: country, with the value "usa" & a variable: year, with the value 1980. Any help would be greatly appreciated. 我期望每个数据框都将有一个新变量:country(国家),值为“ usa”;变量:year(国家),值为1980。我们将不胜感激。

Have you tried something like: 您是否尝试过类似的方法:

usalist <- list.files(path ="~/Desktop/reports/usa")

for(i in 1:length(usalist)){
  df <- read_excel(path = paste("Desktop/reports/usa/", usalist[i], sep = ""))
  df$country <- "usa"
  df$year <- 1980L
  assign(usalist[i], df); rm(df)
}; rm(i)

Haven't run it so I can't say whether I haven't slipped a typo in by mistake. 还没有运行它,所以我不能说我是否没有误输入错字。

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

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