在数据帧列表中,我需要 Select 名为"id"的变量和包含"duo"的变量.因此,我将在输出中 for each datafrmae设置两个变量.
data <- list(foo = structure(list(bodyPart = c("leg", "arm", "knee"),
side = c("LEFT", "RIGHT", "LEFT"), device = c("LLI", "LSM",
"GHT"), `Duo:length` = c(12, 476, 7), id = c("AA", "BB", "CC"),
mwID = c("a12", "k87", "j98")), class = "data.frame", row.names = c(NA,
-3L)), bar = structure(list(bodyPart = c("ankel", "ear", "knee"
), `Duo:side` = c("LEFT", "LEFT", "LEFT"), device = c("GOM", "LSM",
"YYY"), id = c("ZZ", "DD", "FF"), tqID = c("kj8", "ll23", "sc26"
)), class = "data.frame", row.names = c(NA, -3L)))
请注意,情况并非如此.
output <- list(foo = structure(list(`Duo:length` = c(12, 476, 7), id = c("AA", "BB", "CC")),
class = "data.frame", row.names = c(NA, -3L)),
bar = structure(list(`Duo:side` = c("LEFT", "LEFT", "LEFT"), id = c("ZZ", "DD", "FF")),
class = "data.frame", row.names = c(NA, -3L)))
下面的代码只生成id列.我不知道为什么它不能得到包括Duo在内的专栏.
lapply(data_list, function(cr) cr %>% dplyr::select(id, where(~any(grepl("Duo", names(.))))))
我非常感谢你的建议.