【发布时间】:2018-06-06 02:13:40
【问题描述】:
我有一个列表列表。每个列表中的一个元素的名称以“n_”开头。如何提取这些元素并将它们存储在单独的列表中?我可以同时使用map 和starts_with 吗?
例如:
m1 <- list(n_age = c(19,40,39),
names = c("a", "b", "c"))
m2 <- list(n_gender = c("m","f","f"),
names = c("f", "t", "d"))
nice_list <- list(m1, m2)
我希望像下面这样的东西可以工作(它没有!):
output <- map(nice_list, starts_with("n_"))
【问题讨论】:
-
lapply(nice_list, function(x) x[grepl("^n_", names(x))])in base R