R 在每个 sheet 上应用 row_to_names 函数后组合多个 excel sheets
R combine multiple excel sheets after applying row_to_names function over each sheet
我想在 excel 文件的 r 中合并多个 sheets,对于每个 sheet,在合并之前,应用操作 a(每个 sheet 有header 行上方的单元格 a1 中的唯一 ID 名称 - 操作 a 删除它,并使用该值创建一个新的 ID 列(感谢@akrun))。一旦为每个 sheet 完成此操作,我会喜欢结合使用操作 b:
#operation a
#this works for one sheet, removes value in cell a1 and uses as value in new id column
library(openxlsx)
library(dplyr)
library(tidyr)
df1 <- read.xlsx("mydata.xlsx")
df1 %>%
row_to_names(1) %>%
mutate(id = colnames(df1)[1])
#operation b
#this combines all the sheets but I would like operation a to be applied to each sheet first
library(tidyverse)
library(readxl)
combined <- excel_sheets("mydata.xlsx") %>%
map_df(~read_xlsx("mydata.xlsx",.))
如何组合这些操作?
您可以创建一个函数并在 map
中使用它。
library(dplyr)
library(janitor)
library(readxl)
change_column_names <- function(df1) {
df1 %>%
row_to_names(1) %>%
mutate(id = colnames(df1)[1])
}
excel_sheets("mydata.xlsx") %>%
purrr::map_df(~read_xlsx("mydata.xlsx", .x) %>% change_column_names)
我想在 excel 文件的 r 中合并多个 sheets,对于每个 sheet,在合并之前,应用操作 a(每个 sheet 有header 行上方的单元格 a1 中的唯一 ID 名称 - 操作 a 删除它,并使用该值创建一个新的 ID 列(感谢@akrun))。一旦为每个 sheet 完成此操作,我会喜欢结合使用操作 b:
#operation a
#this works for one sheet, removes value in cell a1 and uses as value in new id column
library(openxlsx)
library(dplyr)
library(tidyr)
df1 <- read.xlsx("mydata.xlsx")
df1 %>%
row_to_names(1) %>%
mutate(id = colnames(df1)[1])
#operation b
#this combines all the sheets but I would like operation a to be applied to each sheet first
library(tidyverse)
library(readxl)
combined <- excel_sheets("mydata.xlsx") %>%
map_df(~read_xlsx("mydata.xlsx",.))
如何组合这些操作?
您可以创建一个函数并在 map
中使用它。
library(dplyr)
library(janitor)
library(readxl)
change_column_names <- function(df1) {
df1 %>%
row_to_names(1) %>%
mutate(id = colnames(df1)[1])
}
excel_sheets("mydata.xlsx") %>%
purrr::map_df(~read_xlsx("mydata.xlsx", .x) %>% change_column_names)