R语言 按阈值筛选多个列表

vx6bjr1n  于 2023-05-20  发布在  其他
关注(0)|答案(3)|浏览(183)

我有这个列表列表:

df.list <- list(`2023-02-15_10-30-49` = list(V4 = c(5019.00214, 5019.00215, 
5019.00216, 5019.00217, 5019.00218, 5019.00214, 5019.00215, 5019.00215, 
5019.00215, 5019.00215, 5019.00215, 5019.00215, 5019.00214, 5019.00214
), V6 = c(500.86491, 500.86495, 500.86497, 500.86498, 500.86499, 
500.86375, 500.86374, 500.86372, 500.86372, 500.86372, 500.86373, 
500.86372, 500.86373, 500.86372), V8 = c(235.314, 235.423, 235.502, 
235.556, 235.606, 230.496, 230.502, 230.561, 230.556, 230.553, 
230.564, 230.555, 230.59, 230.598), V10 = c(0.38, 0.37, 0.37, 
0.37, 0.36, 0.037, 0.025, 0.014, 0.014, 0.014, 0.014, 0.014, 
0.014, 0.014), V11 = c(0.75, 0.73, 0.71, 0.7, 0.69, 0.056, 0.04, 
0.017, 0.015, 0.015, 0.015, 0.015, 0.014, 0.015)), `2023-02-15_10-33-16` = list(
    V4 = c(5019.00198, 5019.00194, 5019.00193, 5019.00193, 5019.00216, 
    5019.00194, 5019.00194, 5019.00194, 5019.00194, 5019.00194, 
    5019.00194, 5019.00194, 5019.00194, 5019.00194, 5019.00194, 
    5019.00195, 5019.00195, 5019.00195), V6 = c(500.86428, 500.86419, 
    500.86421, 500.86423, 500.86392, 500.86343, 500.86341, 500.86341, 
    500.86342, 500.86342, 500.86342, 500.86342, 500.86342, 500.86342, 
    500.86342, 500.86344, 500.86343, 500.86343), V8 = c(233.154, 
    233.162, 233.155, 233.142, 230.303, 230.491, 230.464, 230.457, 
    230.466, 230.474, 230.472, 230.482, 230.479, 230.476, 230.488, 
    230.496, 230.497, 230.499), V10 = c(0.37, 0.37, 0.37, 0.37, 
    1.2, 0.014, 0.014, 0.014, 0.014, 0.014, 0.014, 0.014, 0.015, 
    0.015, 0.014, 0.014, 0.014, 0.014), V11 = c(0.6, 0.6, 0.6, 
    0.6, 0.82, 0.017, 0.017, 0.019, 0.021, 0.021, 0.022, 0.022, 
    0.024, 0.023, 0.021, 0.019, 0.018, 0.019)), `2023-02-15_10-33-46` = list(
    V4 = c(5019.00175, 5019.00175, 5019.00175, 5019.00175, 5019.00175, 
    5019.00174, 5019.00175, 5019.00175, 5019.00175, 5019.00174, 
    5019.00173, 5019.00173), V6 = c(500.86318, 500.86319, 500.86318, 
    500.86318, 500.86318, 500.86318, 500.86315, 500.86315, 500.86315, 
    500.86315, 500.86313, 500.86312), V8 = c(230.518, 230.525, 
    230.514, 230.515, 230.531, 230.539, 230.507, 230.506, 230.509, 
    230.513, 230.502, 230.486), V10 = c(0.016, 0.018, 0.018, 
    0.019, 0.021, 0.098, 0.015, 0.014, 0.014, 0.014, 0.014, 0.014
    ), V11 = c(0.023, 0.026, 0.028, 0.029, 0.035, 0.15, 0.024, 
    0.022, 0.021, 0.019, 0.02, 0.022)))

我想用一个阈值来过滤列表,并将输出作为 Dataframe 或列表的列表。为此,我使用了以下代码:

df.list <- lapply(df.list, function(i) filter(i, V10 <= 0.025))

不幸的是,当我应用这个代码时,我得到了这个错误消息:

Error in UseMethod("filter") : 
no applicable method for 'filter' applied to an object of class "list"
kq4fsx7k

kq4fsx7k1#

您有一个列表列表,需要将其转换为 Dataframe 列表。使用purrrdplyr,我们可以执行以下操作:

library(purrr)
library(dplyr)

map(df.list, ~bind_cols(.x) %>% filter(V10 <= 0.025))
#> $`2023-02-15_10-30-49`
#> # A tibble: 8 × 5
#>      V4    V6    V8   V10   V11
#>   <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 5019.  501.  231. 0.025 0.04 
#> 2 5019.  501.  231. 0.014 0.017
#> 3 5019.  501.  231. 0.014 0.015
#> 4 5019.  501.  231. 0.014 0.015
#> 5 5019.  501.  231. 0.014 0.015
#> 6 5019.  501.  231. 0.014 0.015
#> 7 5019.  501.  231. 0.014 0.014
#> 8 5019.  501.  231. 0.014 0.015
#> 
#> $`2023-02-15_10-33-16`
#> # A tibble: 13 × 5
#>       V4    V6    V8   V10   V11
#>    <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 5019.  501.  230. 0.014 0.017
#>  2 5019.  501.  230. 0.014 0.017
#>  3 5019.  501.  230. 0.014 0.019
#>  4 5019.  501.  230. 0.014 0.021
#>  5 5019.  501.  230. 0.014 0.021
#>  6 5019.  501.  230. 0.014 0.022
#>  7 5019.  501.  230. 0.014 0.022
#>  8 5019.  501.  230. 0.015 0.024
#>  9 5019.  501.  230. 0.015 0.023
#> 10 5019.  501.  230. 0.014 0.021
#> 11 5019.  501.  230. 0.014 0.019
#> 12 5019.  501.  230. 0.014 0.018
#> 13 5019.  501.  230. 0.014 0.019
#> 
#> $`2023-02-15_10-33-46`
#> # A tibble: 11 × 5
#>       V4    V6    V8   V10   V11
#>    <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 5019.  501.  231. 0.016 0.023
#>  2 5019.  501.  231. 0.018 0.026
#>  3 5019.  501.  231. 0.018 0.028
#>  4 5019.  501.  231. 0.019 0.029
#>  5 5019.  501.  231. 0.021 0.035
#>  6 5019.  501.  231. 0.015 0.024
#>  7 5019.  501.  231. 0.014 0.022
#>  8 5019.  501.  231. 0.014 0.021
#>  9 5019.  501.  231. 0.014 0.019
#> 10 5019.  501.  231. 0.014 0.02 
#> 11 5019.  501.  230. 0.014 0.022
lf5gs5x2

lf5gs5x22#

也许我们可以尝试嵌套lapply

lapply(
    df.list,
    \(x) lapply(
        x,
        `[`,
        x$V10<=0.025
    )
)

它给出了

$`2023-02-15_10-30-49`
$`2023-02-15_10-30-49`$V4
[1] 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002

$`2023-02-15_10-30-49`$V6
[1] 500.8637 500.8637 500.8637 500.8637 500.8637 500.8637 500.8637 500.8637

$`2023-02-15_10-30-49`$V8
[1] 230.502 230.561 230.556 230.553 230.564 230.555 230.590 230.598

$`2023-02-15_10-30-49`$V10
[1] 0.025 0.014 0.014 0.014 0.014 0.014 0.014 0.014

$`2023-02-15_10-30-49`$V11
[1] 0.040 0.017 0.015 0.015 0.015 0.015 0.014 0.015

$`2023-02-15_10-33-16`
$`2023-02-15_10-33-16`$V4
 [1] 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002
 [9] 5019.002 5019.002 5019.002 5019.002 5019.002

$`2023-02-15_10-33-16`$V6
 [1] 500.8634 500.8634 500.8634 500.8634 500.8634 500.8634 500.8634 500.8634
 [9] 500.8634 500.8634 500.8634 500.8634 500.8634

$`2023-02-15_10-33-16`$V8
 [1] 230.491 230.464 230.457 230.466 230.474 230.472 230.482 230.479 230.476
[10] 230.488 230.496 230.497 230.499

$`2023-02-15_10-33-16`$V10
 [1] 0.014 0.014 0.014 0.014 0.014 0.014 0.014 0.015 0.015 0.014 0.014 0.014
[13] 0.014

$`2023-02-15_10-33-16`$V11
 [1] 0.017 0.017 0.019 0.021 0.021 0.022 0.022 0.024 0.023 0.021 0.019 0.018
[13] 0.019

$`2023-02-15_10-33-46`
$`2023-02-15_10-33-46`$V4
 [1] 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002 5019.002
 [9] 5019.002 5019.002 5019.002

$`2023-02-15_10-33-46`$V6
 [1] 500.8632 500.8632 500.8632 500.8632 500.8632 500.8632 500.8632 500.8632
 [9] 500.8632 500.8631 500.8631

$`2023-02-15_10-33-46`$V8
 [1] 230.518 230.525 230.514 230.515 230.531 230.507 230.506 230.509 230.513
[10] 230.502 230.486

$`2023-02-15_10-33-46`$V10
 [1] 0.016 0.018 0.018 0.019 0.021 0.015 0.014 0.014 0.014 0.014 0.014

$`2023-02-15_10-33-46`$V11
 [1] 0.023 0.026 0.028 0.029 0.035 0.024 0.022 0.021 0.019 0.020 0.022
vd8tlhqk

vd8tlhqk3#

试试看

lapply(df.list, \(x) subset(as.data.frame(x), V10 <= 0.025))

相关问题