如何编辑下面的
R
管道,以便在将所有工作表读入单个数据框之前可以更改列名称?
目前,set_names
函数应用于Excel中的工作表名称,而不是每个工作表中的数据列。有3张表,每张表有4列数据。
library(purrr)
library(readxl)
file_path <- "Test.xlsx"
dfraw <- file_path %>%
excel_sheets() %>%
set_names(., nm = c('A','B','C','D')) %>% #this line shows the error.
map_dfr(.f = ~read_excel(path = file_path, sheet = .x), .id = "Currency")
错误信息:
Error in `set_names()`:
! The size of `nm` (4) must be compatible with the size of `x` (3).
在
set_names
函数中使用 map_dfr
。
library(dplyr)
library(purrr)
library(readxl)
file_path <- "Test.xlsx"
dfraw <- file_path %>%
excel_sheets() %>%
map_dfr(.f = ~read_excel(path = file_path, sheet = .x) %>%
set_names(nm = c('A','B','C','D')), .id = "Currency")
请注意,
map_dfr
已被取代,建议使用map
+ list_rbind
。