从excel工作簿读取时,会创建数据框,但列名不会更改

时间:2017-08-29 06:40:50

标签: r readxl

我正在尝试在R中读取excel工作簿,并且每张工作表都会创建一个数据帧。

在下一步中,我想阅读创建的数据框,并在相应数据框中的每一列之前使用工作表名称和低分。

这是我正在做的事情:

library(readxl)

# Store Sheet Names in a vector
sheet <- excel_sheets("D:/OTC/JULY DATA.XLSX")

# Trim any of the Trailing White Spaces
sheet_trim_trailing <- function (x) sub("\\s+$", "", x)
sheet <- sheet_trim_trailing(sheet)

# Read each of the sheets in the workbook and create a 
# dataframe using respective names of the sheets

for(i in 1:length(sheet)){
  # this read a sheet and create the dataframe using its name
  assign(sheet[i], read.xlsx("DATA.XLSX", sheetIndex = i))
  # store dataframe name into a vector
  sname <- sheet[i]
  # use vector to change the col names in the respective dataframe
  colnames(sname) <- gsub("^", paste0(sname,"_"), colnames(sname))
}

是否已创建数据框但列名称未更改?

我不知道我哪里错了?

2 个答案:

答案 0 :(得分:2)

你需要做的是像

colnames(get(sheet[i])) <- gsub("^", paste0(sname,"_"), colnames(get(sheet[i])))

但这会产生错误

target of assignment expands to non-language object

解决方法是使用临时变量来更改列名

可重复的例子

temp <- mtcars[1:5,]
d <- get("temp")
colnames(d) <- sub("y", " ", colnames(d))
assign("temp", d)

试试这个

for(i in 1:length(sheet)){
  assign(sheet[i], read.xlsx("DATA.XLSX", sheetIndex = i))
  t <- get(sheet[i])
  colnames(t) <- gsub("^", paste0(sheet[i],"_"), colnames(t))
  assign(sheet[i], t)
}

答案 1 :(得分:1)

我想我正在寻找像这样的东西,它与上面的一样。

尝试此替代方案:

library(readxl)

# function to read all the sheets from excel workbook
read_all_sheets <- function(xlsfile) {
  sheets <- excel_sheets(xlsfile)
  setNames(lapply(sheets, function(.) {
    tbl <- read_excel(xlsfile, sheet = .)
    # this will change the col names with sheet name 
    #  and underscore as prefix
    names(tbl) <- paste(., names(tbl), sep = "_")
    tbl
  }), sheets)
}

## create dataframes from sheets
# first read all the sheets are list
List_of_All_Sheets <- read_all_sheets("Location/of/the/file.xlsx")
# then create dataframes
lapply(names(List_of_All_Sheets), 
       function(nams) assign(nams, List_of_All_Sheets[[nams]], 
                             envir = .GlobalEnv))