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

时间:2016-07-14 21:09:23

标签: r dataframe save rename names

所以我编写了一个脚本来对数据框进行更改,但是我遇到了一些我似乎无法解决的问题。首先,我想将列变量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

dataframe 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

dataframe 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

1 个答案:

答案 0 :(得分:1)

试试这个,不再需要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
}