简体   繁体   English

在R中导入多个Excel文件时如何添加文件名?

[英]How to add filename when importing multiple Excel files in R?

I am new to R and learning as I go.我是 R 的新手,并且像我一样学习 go。 I'm trying to import a series of Excel files and then export them as one single file.我正在尝试导入一系列 Excel 文件,然后将它们导出为一个文件。 The code below works fine for that.下面的代码可以正常工作。 However, I cannot figure out how to add a filename to each row of the data that corresponds to the filename it came from.但是,我无法弄清楚如何将文件名添加到与它来自的文件名相对应的每一行数据中。 The filenames are in a pattern "YYYYMMDD-....." and I would like to crop that so only the YYYYMMDD is saved as the filename.文件名采用“YYYYMMDD-.....”模式,我想裁剪它,所以只有 YYYYMMDD 被保存为文件名。

*# Import series of files from Excel
library(readxl)
file.list <- list.files(pattern='*.xls')
df.list <- lapply(file.list, read_excel)

# Combine all Excel files into one dataframe
library(dplyr)
df <- bind_rows(df.list, .id = "id")

# Export to Excel
library(writexl)
write_xlsx(df,"my file path goes here")

Any help you can provide would really help me from throwing my laptop across the room!您可以提供的任何帮助都将真正帮助我避免将笔记本电脑扔到房间的另一边! Thanks!谢谢!

should be fairly straight forward:应该相当直截了当:


library(readxl)
library(purrr)
library(stringr)
file.list <- list.files(pattern='*.xls')
df <- map_dfr( file.list, ~ read_excel(.x) %>% mutate( File = str_match( .x, "\\d{8}" )[,1] ) )


Note I don't have a set of xls files to try this on.注意我没有一组 xls 文件来尝试这个。

Assuming that your vector of file names, file.list , looks something like this:假设您的文件名向量file.list看起来像这样:

file.list <- c("20200101-foo1.xls", "20210101-foo2.xls")

The trick is to make df.list a named list.诀窍是使df.list成为一个命名列表。 You can use stringr::str_replace to make the YYYYMMDD names.您可以使用stringr::str_replace来制作 YYYYMMDD 名称。 In this example, remove everything from the "-" onwards:在本例中,从“-”开始删除所有内容:

library(stringr)
file.list %>% str_replace("-.+", "")
[1] "20200101" "20210101"

So supply the names to the list like this:因此,将名称提供给列表,如下所示:

names(df.list) <- file.list %>% 
  str_replace("-.+", "")

And now bind_rows with the .id argument will create a column with the names:现在带有.id参数的bind_rows将创建一个具有名称的列:

df <- bind_rows(df.list, .id = "filename")

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

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