基于特定列表元素筛选列表



我有一个列表,其中包含以下内容:

$AKAM
Augmented Dickey-Fuller Test
data:  Cl(.)
Dickey-Fuller = -3.6785, Lag order = 3, p-value = 0.03802
alternative hypothesis: stationary

$ALXN
Augmented Dickey-Fuller Test
data:  Cl(.)
Dickey-Fuller = -2.9311, Lag order = 3, p-value = 0.2052
alternative hypothesis: stationary

我想根据p-value进行筛选。我可以使用:d$AKAM$p.value访问p-value。我想基于p值是<0.05.

数据:

d <- list(ABMD = structure(list(statistic = c(`Dickey-Fuller` = -2.88823930907752), 
parameter = c(`Lag order` = 3), alternative = "stationary", 
p.value = 0.222153876057991, method = "Augmented Dickey-Fuller Test", 
data.name = "Cl(.)"), class = "htest"), ATVI = structure(list(
statistic = c(`Dickey-Fuller` = -2.86560166108736), parameter = c(`Lag order` = 3), 
alternative = "stationary", p.value = 0.231115731952747, 
method = "Augmented Dickey-Fuller Test", data.name = "Cl(.)"), class = "htest"), 
ADBE = structure(list(statistic = c(`Dickey-Fuller` = -2.91292875187715), 
parameter = c(`Lag order` = 3), alternative = "stationary", 
p.value = 0.212379749850693, method = "Augmented Dickey-Fuller Test", 
data.name = "Cl(.)"), class = "htest"), AMD = structure(list(
statistic = c(`Dickey-Fuller` = -3.46654255102478), parameter = c(`Lag order` = 3), 
alternative = "stationary", p.value = 0.0592779965240372, 
method = "Augmented Dickey-Fuller Test", data.name = "Cl(.)"), class = "htest"), 
AKAM = structure(list(statistic = c(`Dickey-Fuller` = -3.67846516682619), 
parameter = c(`Lag order` = 3), alternative = "stationary", 
p.value = 0.0380202892654308, method = "Augmented Dickey-Fuller Test", 
data.name = "Cl(.)"), class = "htest"), ALXN = structure(list(
statistic = c(`Dickey-Fuller` = -2.93110833009175), parameter = c(`Lag order` = 3), 
alternative = "stationary", p.value = 0.205182767184581, 
method = "Augmented Dickey-Fuller Test", data.name = "Cl(.)"), class = "htest"))
sapply(d, function(x) x$p.value < 0.05)
#  ABMD  ATVI  ADBE   AMD  AKAM  ALXN 
# FALSE FALSE FALSE FALSE  TRUE FALSE 

从列表中筛选数据

d[sapply(d, function(x) x$p.value < 0.05)]
# $AKAM
# 
# Augmented Dickey-Fuller Test
# 
# data:  Cl(.)
# Dickey-Fuller = -3.6785, Lag order = 3, p-value = 0.03802
# alternative hypothesis: stationary

我们可以从base R使用Filter

Filter(function(x) x$p.value < 0.05, d)
#$AKAM
#   Augmented Dickey-Fuller Test
#data:  Cl(.)
#Dickey-Fuller = -3.6785, Lag order = 3, p-value = 0.03802
#alternative hypothesis: stationary

或来自purrrkeep

library(purrr)
keep(d, ~ .x$p.value < 0.05)

涉及purrr的一个选项可能是:

discard(d, ~ .x$p.value > 0.05)
$AKAM
Augmented Dickey-Fuller Test
data:  Cl(.)
Dickey-Fuller = -3.6785, Lag order = 3, p-value = 0.03802
alternative hypothesis: stationary

最新更新