将列表中元素的名称与文件名匹配并重命名 R 中的变量

Matching names of elements in a list to filenames & renaming variables in R

所以我写了一个脚本来更改数据帧,但是我遇到了一些我似乎无法解决的问题。首先,我想将列变量 mac_sector 重命名为扇区的部分似乎不起作用,它不会重命名任何东西,也不会报错。

另外,当我保存修改后的数据集时,它们被简单地称为 1,2,3...等等。但是,实际上我只是希望它们具有与原来相同的名称。我尝试通过 "names(dflist)[i] <- gsub("\.dta$", "", files)" 执行此操作,但那行不通。

它也给出了这些警告消息,虽然我不知道它们是否对文件有实际影响,因为我没有看到任何并发症: 警告信息:

1: In save.dta13(dflist[[i]], paste0(i, ".dta")) :
  Number of variable labels does not match number of variables.
            Variable labels dropped.
2: In save.dta13(dflist[[i]], paste0(i, ".dta")) :
  Number of variable labels does not match number of variables.
            Variable labels dropped.
3: In save.dta13(dflist[[i]], paste0(i, ".dta")) :
  Number of variable labels does not match number of variables.
            Variable labels dropped. 

最后,有没有办法将文件保存到工作目录以外的其他目录?

我的脚本:

setwd("C:\....")

    files = list.files(pattern="*.dta") 
    dflist <- list()

    for (i in 1:length(files)){
      dflist[[i]] <- read.dta13(files[i], nonint.factors = TRUE)


      if("mac_sector" %in% colnames(dflist[[i]])){            #rename mac_sector to sector if present   
        rename(dflist[[i]], c(mac_sector="sector"))}

      if(!("sector" %in% colnames(dflist[[i]]))){             #This creates "sector" variable if it doesn't exist already.
        dflist[[i]]$sector <- "total"}


      names(dflist)[i] <- gsub("\.dta$", "", files)          #Matching the names of the elements to the filenames

      save.dta13(dflist[[i]], paste0(i, ".dta"))              #Saving dataset
    }

输入: 数据框 1:

country     SA year          DV       VI     DI       DIV     DIV_s  DIV_p                  t            ta               
1  AUSTRIA   NA 2001         0      NA       NA      NA     NA       NA                  0               NA
2  AUSTRIA   NA 2002         0      NA       NA      NA     NA       NA                  0               NA
3  AUSTRIA   NA 2003         0      NA       NA      NA     NA       NA                  0               NA
4  AUSTRIA   NA 2004         0      NA       NA      NA     NA       NA                  0               NA
5  AUSTRIA   NA 2005         0      NA       NA      NA     NA       NA                  0               NA

数据帧 2:

country      mac_sector      SA year          DV       VI     DI       DIV     DIV_s  DIV_p                  t            ta 
1  BELGIUM     ing            0 2001         0      NA       NA      NA     NA       NA               3036       0.09725133
2  BELGIUM     ing            0 2002         0      NA       NA      NA     NA       NA               2970       0.09641831
3  BELGIUM     ing            0 2003         0      NA       NA      NA     NA       NA               2917       0.09791633
4  BELGIUM     ing            0 2004         0      NA       NA      NA     NA       NA               2907       0.10297798
5  BELGIUM     ing            0 2005         0      NA       NA      NA     NA       NA               2904       0.10182869

数据帧 3:

country                       sector SA year          DV       VI     DI       DIV     DIV_s  DIV_p                  t            ta
1  BELGIUM                        prod     0 2001         0      NA       NA      NA     NA       NA                392       0.09688306
2  BELGIUM                        prod     0 2002         0      NA       NA      NA     NA       NA                398       0.09394456
3  BELGIUM                        prod     0 2003         0      NA       NA      NA     NA       NA                394       0.09536502
4  BELGIUM                        prod     0 2004         0      NA       NA      NA     NA       NA                404       0.10367264
5  BELGIUM                        prod     0 2005         0      NA       NA      NA     NA       NA                407       0.08961585

试试这个,不再需要 plyr 库,应该能够重命名并以您想要的文件名保存到新位置:

setwd("C:\...")
files = list.files(pattern="*.dta") 
dflist <- list()

for (i in 1:length(files)){
  dflist[[i]] <- read.dta13(files[i],header=TRUE)

  if("mac_sector" %in% colnames(dflist[[i]])){            #rename mac_sector to sector if present   
    names(dflist[[i]])[names(dflist[[i]])=="mac_sector"] <- "sector"
    #rename(dflist[[i]], replace = c("mac_sector"="sector"))}

  if(!("sector" %in% colnames(dflist[[i]]))){             #This creates "sector" variable if it doesn't exist already.
    dflist[[i]]$sector <- "total"}


names(dflist)[i] <- gsub("\.dta$", "", files[i])          #Matching the names of the elements to the filenames

save.dta13(dflist[[i]],paste0("C:\...\newlocation\",names(dflist)[i], ".dta"))              #Saving dataset
}