简体   繁体   English

为R中文件夹中的所有excel文件重命名同一列

[英]Rename the same column for all excel files in a folder in R

I have multiple excel files in a folder, so I want to clean each file in the folder, then append all the excel files together.我在一个文件夹中有多个 excel 文件,所以我想清理文件夹中的每个文件,然后将所有 excel 文件附加在一起。 I want to rename the first column of all excel files so I am using the following codes :我想重命名所有 excel 文件的第一列,所以我使用以下代码:

filelist<- list.files(pattern="*.xlsx")
DF <- lapply(filelist,function(i) {
Fu <- read_excel(i, sheet="XX")
colnames(Fu[[i]])[1] <- "Column 1"})

However, I got an error message: Error in colnames<- ( *tmp* , value = "SCENARIO_KEY") : attempt to set 'colnames' on an object with less than two dimensions.但是,我收到一条错误消息: colnames<- ( *tmp* , value = "SCENARIO_KEY") 中的错误:尝试在小于二维的对象上设置“colnames”。 How to fix it?如何解决? Thank you.谢谢你。

In the lapply , itself, we can rename.lapply本身,我们可以重命名。 The i in the loop are each of the element of 'filelist'.循环中的i是“filelist”的每个元素。 It cannot be used for subsetting 'Fu'.它不能用于子集 'Fu'。 Instead, we can directly subset the column based on the index as 'Fu' is a single data.frame within each list element相反,我们可以根据索引直接对列进行子集,因为“Fu”是每个list元素中的单个 data.frame

DF <- lapply(filelist,function(i) {
          Fu <- read_excel(i, sheet="XX")
          colnames(Fu)[1] <- "Column 1"
          Fu
     })

Even if the index is right, when we subset a data.frame with [[ to get the column, it returns a vector and there is dim or column names attribute for a vector即使索引是正确的,当我们使用[[对 data.frame 进行子集化以获取列时,它会返回一个vector并且vector有 dim 或 column names 属性

mtcars[[1]]

is a vector是一个vector

and doing the assignment returns the error because the colnames is NULL并且执行分配会返回错误,因为 colnames 为NULL

colnames(mtcars[[1]])
#NULL

colnames(mtcars[[1]]) <- "new column"

Error in colnames<- ( *tmp* , value = "new column") : attempt to set 'colnames' on an object with less than two dimensions colnames<- ( *tmp* , value = "new column") 中的错误:尝试在小于二维的对象上设置“colnames”

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

相关问题 使用 R 在文件夹中的所有 excel 文件(以及 excel 文件中的所有工作表)中写入新列 - Writing a new column in all excel files (and all sheets in an excel file) in a folder using R 如何使用R中列表列中的名称重命名文件夹中的文件列表 - How to rename a list of files in a folder with names in a list column in R 在 R 中创建迭代循环以读取文件夹中的所有 Excel 文件,执行相同的 function,并导出为新文件 - Creating an iterative loop in R to read in all Excel files in a folder, performing the same function, and exporting as a new file 如何将所有Excel文件的名称从文件夹打印到R中的CSV? - How to print the names of all excel files from a folder to a csv in R? 如何重命名以“.csv”结尾的文件夹中的文件??[R] - How to rename files in a folder with ".csv" at the end??[R] 用R重命名Windows文件夹中的几个文件 - Rename several files in windows folder with R 按表格将所有Excel文件读入R中,文件名作为列 - Read All Excel Files into R by Sheet with file name as column 重命名 R 中列中的所有值 - Rename all the values within a column in R R批处理文件夹中的所有文件 - R batch all files in folder R - 重命名列标题中匹配字符串右侧的所有列标题 - R - Rename all column titles right of matching string in column title
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM