簡體   English   中英

將多個 excel 文件導入 R 中的一個 df,同時向每個單獨的文件添加一個新列

[英]Importing multiple excel files into one df in R whilst adding a new column to each individual file at the same time

我正在將多個 excel 文件作為 df 上傳到 R (每個文件中的多個工作表之一,都具有相同的格式)。 在上傳要在數據框中顯示的文件時,我需要添加一個額外的列。 我的原始數據中有一個名為“反饋”的列。 我需要在正在上傳的每張工作表(即主題)的第一行中添加一列,將“反饋”中的值向下移動一個(創建 NA)。

我將數據導入 R 的代碼如下所示:

files <- dir(
  path = "my path",
  pattern = ".xlsx",
  full.names = TRUE)


# Store the names of the sheets to be accessed, using one of the files
# as the template for the sheet names
all_sheets <- excel_sheets(files[1])

# .x is the vector of the file paths. map_df gives a single data frame/tibble as its output
# "for each file path..."

merged_files <- files %>% map_dfr(read_xlsx, .id = "sbj_no", sheet = all_sheets[2]) %>%
  select(response, time, feedback, target, sbj_no)

我無法弄清楚如何在導入數據的過程中添加/創建所需的列。 任何幫助將不勝感激!

謝謝!

這是一種將每個工作簿中的所有工作表合並為一個並為工作簿名稱創建列和工作表名稱列的方法:

library(plyr)
library(dplyr)
library(purrr)
library(readxl)

files <- list.files(path="C:\\",pattern='*.xlsx')
wbs <- paste("C:\\", files, sep="\\")

read_multiple_excel <- function(path) {
    path %>%
    excel_sheets() %>% 
    set_names() %>% 
    map_df(~read_excel(path=path, sheet = .x, ..., .id = "sheet")
}

data_df <- wbs %>%
    set_names() %>%
    map_df(read_multiple_excel, .id = "wb")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM