读取Excel文件的文件夹,并将单个表作为单独的df&extim

发布于 2025-02-09 08:15:24 字数 998 浏览 1 评论 0原文

我有一个excel文件的文件夹,每个文件都包含多个图纸。每个WB中的床单都相同。我正在尝试将所有Excel文件作为单独的数据帧导入一个特定的命名表。我已经能够进口它们;但是,名称成为df_1,df_2,df_3等...我一直在尝试将Excel文件名的第一个单词命名,并使用它来识别DF。

Excel文件名称“ AAPL多片”的示例该表将被命名为“ balance”,我将作为DF导入。结果,我希望“ AAPL Balance DF”。

但是,最接近我所在的代码,但是,它将每个数据框架称为df_1,df_2等。

library(purrr)
library(readxl)

files_list <- list.files(path = 'C:/Users/example/Drive/Desktop/Total_Related_Data/Analysis of Data/',
pattern = "*.xlsx",full.names = TRUE)

files_list %>% 
    walk2(1:length(files_list),
          ~ assign(paste0("df_", .y), read_excel(path = .x), envir = globalenv()))

我尝试在过去0函数中使用文件路径变量'file_list'标记它们并最终出现,

df_c:/ /desktop/total_releated_data/数据/.xlsx2的分析,

依此类推。

我试图列出要使用的文件名列表。这读取文件名并创建了一个列表,但我无法与上面的代码一起使用。

files_Names<-list.files(path='C:/Users/example/Drive/Desktop/Total_Related_Data/Analysis of Data/', pattern=NULL, all.files=FALSE, full.names=FALSE)

因此, 列表中所有文件的data.xlsx的AAPL分析。

I have a folder of excel files that contain multiple sheets each. The sheets are named the same in each wb. I'm trying to import one specific named sheet for all excel files as separate data frames. I have been able to import them in; however, the names become df_1, df_2, df_3, etc... I've been trying to take the first word of the excel file name and use that to identify the df.

Example of Excel file Name "AAPL Multiple Sheets" the sheet would be named "Balance" I'm importing as a df. I would like "AAPL Balance df" as the result.

The code that came closest to what I'm looking for located below, however, it names each data frame as df_1, df_2, and so on.

library(purrr)
library(readxl)

files_list <- list.files(path = 'C:/Users/example/Drive/Desktop/Total_Related_Data/Analysis of Data/',
pattern = "*.xlsx",full.names = TRUE)

files_list %>% 
    walk2(1:length(files_list),
          ~ assign(paste0("df_", .y), read_excel(path = .x), envir = globalenv()))

I tried using the file path variable 'file_list' in the past0 function to label them and ended up with,

df_C:/Users/example/Drive/Desktop/Total_Related_Data/Analysis of Data/.xlsx1, df_C:/Users/example/Drive/Desktop/Total_Related_Data/Analysis of Data/.xlsx2,

and so on.

I tried to make a list of file names to use. This read the file names and created a list but I couldn't make it work with the code above.

files_Names<-list.files(path='C:/Users/example/Drive/Desktop/Total_Related_Data/Analysis of Data/', pattern=NULL, all.files=FALSE, full.names=FALSE)

Which resulted with this,
"AAPL Analysis of Data.xlsx" for all the files in the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荆棘i 2025-02-16 08:15:24

您可以执行以下操作(请注意,我正在使用OpenXLSX软件包在Excel文件中读取,但是您当然可以用ReadXl替换该部分):

library(openxlsx)
library(tidyverse)

Starting with your `files_list` we can do:

# using lapply to read in all files and store them as list elements in one list
list_of_dfs <- lapply(as.list(files_list), function(x) readWorkbook(x, sheet = "Balance"))

# Create a vector of names based on the first word of the filename + "Balance"
# Note that we can't use empty space in object names, hence the underscore
df_names <- paste0(str_extract(basename(files_list), "[^ ]+"), "_Balance_df")

# Assign the names to our list of dfs
names(list_of_dfs) <- df_names

# Push the list elements (i.e. data frames) to the Global environment
# I highly recommend NOT doing this. I'd say in 99% of the cases it's better to continue working in the list structure or combine the individual dfs into one large df.
list2env(list_of_dfs, env = .GlobalEnv)

You can do the following (note that I'm using the openxlsx package for reading in Excel files, but you can replace that part with readxl of course):

library(openxlsx)
library(tidyverse)

Starting with your `files_list` we can do:

# using lapply to read in all files and store them as list elements in one list
list_of_dfs <- lapply(as.list(files_list), function(x) readWorkbook(x, sheet = "Balance"))

# Create a vector of names based on the first word of the filename + "Balance"
# Note that we can't use empty space in object names, hence the underscore
df_names <- paste0(str_extract(basename(files_list), "[^ ]+"), "_Balance_df")

# Assign the names to our list of dfs
names(list_of_dfs) <- df_names

# Push the list elements (i.e. data frames) to the Global environment
# I highly recommend NOT doing this. I'd say in 99% of the cases it's better to continue working in the list structure or combine the individual dfs into one large df.
list2env(list_of_dfs, env = .GlobalEnv)
深府石板幽径 2025-02-16 08:15:24

我希望我可以在没有代码的情况下重现您的示例。我将创建一个功能,以获得新的文件名的更多控制权。

我建议:

library(purrr)
library(readxl)
library(openxlsx)

target_folder <- 'C:/Users/example/Drive/Desktop/Total_Related_Data/Analysis of Data'

files_list <- list.files(path = target_folder,
                         pattern = "*.xlsx", full.names = TRUE)

tease_out <- function(file) {
  data <- read_excel(file, sheet = "Balance")
  filename <- basename(file) %>% tools::file_path_sans_ext()
  new_filename <- paste0(target_folder, "/", fileneame, "Balance df.xlsx")
  
  write.xlsx(data, file = new_filename)
}

map(file_list, tease_out)

让我知道它是否有效。我认为您只是针对“余额”表的目标?

I hope I could reproduce your example without code. I would create a function to have more control for the new filename.

I would suggest:

library(purrr)
library(readxl)
library(openxlsx)

target_folder <- 'C:/Users/example/Drive/Desktop/Total_Related_Data/Analysis of Data'

files_list <- list.files(path = target_folder,
                         pattern = "*.xlsx", full.names = TRUE)

tease_out <- function(file) {
  data <- read_excel(file, sheet = "Balance")
  filename <- basename(file) %>% tools::file_path_sans_ext()
  new_filename <- paste0(target_folder, "/", fileneame, "Balance df.xlsx")
  
  write.xlsx(data, file = new_filename)
}

map(file_list, tease_out)

Let me know if it works. I assume you are just targeting for the sheet "Balance"?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文