从Excel工作簿的几张纸中选择基于列名的特定列

发布于 2025-02-13 10:33:15 字数 549 浏览 2 评论 0原文

我有一个带有几张纸的Excel工作簿。每张纸代表一年,并包含几列。并非所有的床单都有相同的列名称,但是我想拿出具有所有床单中共享名称的特定列。

我使用以下功能导入表格将其纳入列表中,

    sheets <- readxl::excel_sheets(filename)
    x <- lapply(sheets, function(X) readxl::read_excel(filename, skip = 2, sheet = X))
    if(!tibble) x <- lapply(x, as.data.frame)
    names(x) <- sheets
    x }

现在有一个包含大约7个元素的大列表。我创建了另一个列表,其中包含我想从每个表格中删除的列名

select&lt; -list(c(“平均”,“ total”,“ attry”,“百分比”,“年”))>

我如何使用此列表将每个纸上的各个列将其拉出到新的DF中?

抱歉,如果相当明显,并提前感谢您的任何建议!

I've got an excel workbook with several sheets. Each sheet represents a year and contains several columns. Not all the sheets have the same columns names but I want to pull out specific columns with names that are shared in all sheets.

I've imported the sheet using the following function to make it into a list

    sheets <- readxl::excel_sheets(filename)
    x <- lapply(sheets, function(X) readxl::read_excel(filename, skip = 2, sheet = X))
    if(!tibble) x <- lapply(x, as.data.frame)
    names(x) <- sheets
    x }

I now have a large list containing about 7 elements. I created another list containing the column names I want to pull out from each sheet

select <-list(c("average","total","percentage","year on year"))

How do I use this list to pull out the respective columns from each sheet into a new df?

Sorry if fairly obvious and thanks in advance for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2025-02-20 10:33:15

我们可以选择之后列。另外,只需传递vector列名称

library(purrr)
library(readxl)
nm1 <- c("average","total","percentage","year on year")
map(sheets, ~ readxl::read_excel(filename, skip = 2, sheet = .x) %>%
                dplyr::select(all_of(nm1)) %>%
                 as_tibble))

We may select the columns afterwards. Also, instead of list of element 1, just pass a vector of column names

library(purrr)
library(readxl)
nm1 <- c("average","total","percentage","year on year")
map(sheets, ~ readxl::read_excel(filename, skip = 2, sheet = .x) %>%
                dplyr::select(all_of(nm1)) %>%
                 as_tibble))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文