这是一个示例数据集:
dat1 <- structure(list(id = 1:3, des.1 = 4:6, x = 7:9, not = 10:12), class = "data.frame", row.names = c(NA,-3L))
dat2 <- structure(list(id = 1:3, descript = 4:6, y = 7:9, yes = 10:12), class = "data.frame", row.names = c(NA,-3L))
dat3 <- structure(list(id = 1:3, description = 4:6, x = 7:9, X4 = 10:12), class = "data.frame", row.names = c(NA,-3L))
dat_list <- list(dat1, dat2, dat3)
例如,如果我想删除此数据框列表中所有名为x和X4的列,则可以执行以下操作-访问包含这些列的每个数据框并按如下方式删除它们:
dat_list[[1]] <- dat_list[[1]] %>% select(-x)
dat_list[[3]] <- dat_list[[3]] %>% select(-c("x", "X4"))
但是我想知道是否有一种方法可以在整个列表中执行此操作-请记住,这些列中的某些列将不会出现在列表中的每个数据框中。
谁能想到一种方法来删除此列表中所有数据框内所有名为c(“ x”,“ X4”)的列?
一种方法是:
library(tidyverse)
map(dat_list, function(xx) xx %>% select(any_of(c("x", "X4"))))
# [[1]]
# x
# 1 7
# 2 8
# 3 9
# [[2]]
# data frame with 0 columns and 3 rows
# [[3]]
# x X4
# 1 7 10
# 2 8 11
# 3 9 12
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句