【问题标题】:Joining data in lists of list在列表列表中加入数据
【发布时间】:2020-06-20 06:23:57
【问题描述】:

我正在使用 readxl 包从多个 excel 文件中导入数据,并在我的脚本中创建了一个函数,以便只导入我需要的特定工作表

read_excel_sheets <- function(excelDoc) {
     sheets <- readxl::excel_sheets(excelDoc)
     sheets <- sheets[4:6]
     x <- lapply(sheets, function(X) readxl::read_excel(excelDoc, sheet = X))
     return(x)
}
#load files in folder
rawfiles <- list.files()
IMPORT <- lapply(rawfiles, FUN = read_excel_sheets)

将我文件夹中的文件加载到我的脚本后,IMPORT 变成了一个列表[10],其中包含列表[3],基本上是列表中的列表。

很遗憾,我无法使用 reduce(full_join) 将数据收集到一个数据表中。我尝试只使用一个 excel 文件并使用 unlist() 来查看是否可以将工作表从列表列表中取出,但这不起作用。

Test <- read_excel_sheets("Hop_L_Trial1.xlsx")
Test_Test <- unlist(Test)

我也试过

rawfiles <- list.files()
IMPORT <- lapply(rawfiles,
                FUN = read_excel_sheets) 
Test_3 <- rbindlist(IMPORT) 

并收到错误“第 1 项的第 1 列的长度为 2,与第 2 列的长度为 6 不一致。只有长度为 1 的列被回收。”任何关于如何将我的数据加入一个数据表的建议将不胜感激,谢谢。

【问题讨论】:

    标签: r excel join


    【解决方案1】:

    您可以使用purrr 中的map_df 将数据作为单个数据帧获取。

    read_excel_sheets <- function(excelDoc) {
      sheets <- readxl::excel_sheets(excelDoc)
      sheets <- sheets[4:6]
      x <- purrr::map_df(sheets, function(X) readxl::read_excel(excelDoc, sheet = X))
      return(x)
    }
    
    IMPORT <- purrr::map_df(rawfiles, FUN = read_excel_sheets)
    

    您还可以使用do.call + rbind 基本 R 函数。

    read_excel_sheets <- function(excelDoc) {
      sheets <- readxl::excel_sheets(excelDoc)
      sheets <- sheets[4:6]
      x <- do.call(rbind, lapply(sheets, function(X) readxl::read_excel(excelDoc, sheet = X)))
      return(x)
    }
    
    IMPORT <- do.call(rbind, lapply(rawfiles, FUN = read_excel_sheets))
    

    【讨论】:

    • 我使用了 do.call + rbind 方法,但是我不得不使用 rbind.fill 因为工作表 4 有 5 列,工作表 5&6 有 9 列。现在我的 IMPORT 有 NA,其中工作表与列名不匹配。此外,由于工作表 5/6 具有相同的变量列名,我需要以某种方式区分哪些来自工作表 5,哪些来自工作表 6。
    • @mpvalenc 如果您使用map_df,您可以添加一个“id”列来了解哪个工作表来自哪里。类似x &lt;- purrr::map_df(sheets, function(X) readxl::read_excel(excelDoc, sheet = X), .id = "id")
    • IMPORT 现在在前两个 col(Frame, Time) 中有数据,但是在 Fx、Fy、Fz、Mx、My、Mz 列中有 NA,因为第一张表 (4) 做了没有这些列。当表 4/5 添加到 IMPORT 时,帧、时间有 NA。第 4 页是框架、时间、任务、肢体、试验。 Sheet5&6 是 Fx、Fy、Fz、Mx、My、Mz、任务、肢体、试验。我还想区分工作表 5 列名称并为每个列名称添加一个 L,并为工作表 6 列名称添加一个 R。
    • 这是我现在的 read_excel_sheets 函数: read_excel_sheets % mutate(idstring = excelDoc) %>% 分离(col = idstring, into = c ("task", "limb", "trial"), sep = "_") return(x) }
    • read_excel_sheets
    【解决方案2】:

    对于data.table::rbindlist 方法,只需设置fill=T

    library(data.table)
    
    dt_list = lapply(5:10, function(i) {
      data.table(rnorm(i))
    })
    dt=rbindlist(dt_list, fill=T) 
    

    【讨论】:

      【解决方案3】:

      您可以使用 dplyr 包中的 bind_rows,它是 tidyverse 的一部分:

      x <- list(tibble(x=runif(5)),  tibble(x=runif(5)))
      y <- list(tibble(x=runif(5)),  tibble(x=runif(5)))
      z = list(x, y)
      
      ans <- bind_rows(z)
      ans
      A tibble: 20 x 1
              x
          <dbl>
       1 0.746 
       2 0.0669
       3 0.612 
       4 0.0702
       5 0.990 
       6 0.301 
       7 0.177 
       8 0.799 
       9 0.242 
      10 0.741 
      11 0.651 
      12 0.113 
      13 0.927 
      14 0.506 
      15 0.477 
      16 0.922 
      17 0.217 
      18 0.566 
      19 0.539 
      20 0.327 
      

      如果您需要跟踪每行最初来自列表的哪个元素,请使用(比如)bind_rows(..., .id="Sheet")

      【讨论】:

        猜你喜欢
        • 2015-10-07
        • 2020-05-11
        • 2020-12-12
        • 1970-01-01
        • 2013-12-31
        • 1970-01-01
        • 2021-06-17
        • 2022-06-15
        • 1970-01-01
        相关资源
        最近更新 更多