如何在列表中选择不在顶级的特定项目?

问题描述 投票:0回答:2

如果我只需要降低一个级别,这就是我只能获得“ A”和“ B”:

MyList[["pine"]][c("A", "B")]

我认为这两种方法之一会模仿这一点,但两者都给我错误:
NotC <- map(.x = MyList, 
        .f = \(x) map(x, .f = x[c("A", "B")]))

NotC <- lapply(MyList, \(x) lapply(x, \(y) y[c("A", "B")]))

似乎R认为我正在尝试选择列,因为我会遇到错误,例如``[.data.frame'(y,c(“ a”,“ b”)):选择了未定义的列。 “

	

如果总是称为“ C”,您可以做到这一点。

MyList <- list("pine" = list(A = data.frame(), B = data.frame(), C = hist(rnorm(10))), "fir" = list(A = data.frame(), B = data.frame(), C = hist(rnorm(10))), "cedar" = list(A = data.frame(), B = data.frame(), C = hist(rnorm(10)))) deletec <- function(l) { l[names(l) != "C"] } lapply(MyList, deletec) > $pine $pine$A data frame with 0 columns and 0 rows > > $pine$B data frame with 0 columns and 0 rows > > > $fir $fir$A data frame with 0 columns and 0 rows > > $fir$B data frame with 0 columns and 0 rows > > > $cedar $cedar$A data frame with 0 columns and 0 rows > > $cedar$B data frame with 0 columns and 0 rows
r purrr
2个回答
0
投票

您可以使用元素名称和
setdiff
的功能,然后使用括号函数

"C"

提取。

0
投票


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.