如何从R中的多个类似结构化excel文件中提取注释?
我有1000个结构相同的.xlsx文件。它们都包含标题行id、填写日期、项目1至11以及带有值的行。项目11下的单元格在大多数文件中都包含注释。如何从所有文件中提取注释并将它们合并到R中的单个对象中 通过创建文件列表,我成功地将所有文件合并到一个data.frame中。下面是一种使用purrr的方法: 编辑:修改后的解决方案,为每个注释输出源文件,并处理缺少此类注释的文件,因为OP指定注释存在于大多数文件中如何从R中的多个类似结构化excel文件中提取注释?,r,excel,import,R,Excel,Import,我有1000个结构相同的.xlsx文件。它们都包含标题行id、填写日期、项目1至11以及带有值的行。项目11下的单元格在大多数文件中都包含注释。如何从所有文件中提取注释并将它们合并到R中的单个对象中 通过创建文件列表,我成功地将所有文件合并到一个data.frame中。下面是一种使用purrr的方法: 编辑:修改后的解决方案,为每个注释输出源文件,并处理缺少此类注释的文件,因为OP指定注释存在于大多数文件中 谢谢你,乔恩!它可以工作,stru_detect和unnest解决了一些其他问题。接下来
谢谢你,乔恩!它可以工作,stru_detect和unnest解决了一些其他问题。接下来,除了注释之外,我如何获得对象中每个文件的第1-10项的值?
library(tidyxl)
library(purrr)
# First, here's a list of xlsx files in the directory:
file_list <- list.files() %>%
.[str_detect(., ".xlsx")]
file_list
#[1] "test1.xlsx" "test2.xlsx" "test3 no comment.xlsx"
# Make a new tibble with two columns:
# file_name is the source file we're looking at
# comments extracts the comments in N8, if any
tibble(file_name = file_list,
comments = map(file_list,
~ xlsx_cells(.) %>%
subset(address == "N8", comment))) %>%
unnest(comments, keep_empty = TRUE)
## A tibble: 3 x 2
# file_name comment
# <chr> <chr>
#1 test1.xlsx Comment in file 1
#2 test2.xlsx Comment in file 2
#3 test3 no comment.xlsx NA