基于条件(值)的子集嵌套列表

发布于 2025-01-17 12:06:23 字数 1620 浏览 1 评论 0 原文

我有一个大嵌套列表(命名列表的列表) - 下面给出了此类列表的示例。我想创建一个新列表,其中只能保留包含0和1值的“ CO”向量的子列表,而仅0的唯一sublists才会被丢弃(例如,输出应仅包含第一,第三 - 和第四个亚组。 我按照此线程进行了laply和过滤的效果:

subset列表中的元素基于逻辑条件

但是,它引发了错误。我将感谢您如何处理列表中的列表的提示。

# reprex
set.seed(123)

## empty lists
first_group <- list()
second_group <- list()
third_group <- list()
fourth_group <- list()

# dummy_vecs
values1 <- c(sample(120:730, 30, replace=TRUE))
coeff1 <- c(sample(0:1, 30, replace=TRUE))

values2 <- c(sample(50:810, 43, replace=TRUE))
coeff2 <- c(rep(0, 43))

values3 <- c(sample(510:730, 57, replace=TRUE))
coeff3 <- c(rep(0, 8), rep(1, 4), rep(0, 45))

values4 <- c(sample(123:770, 28, replace=TRUE))
coeff4 <- c(sample(0:1, 28, replace=TRUE))

## fill lists with values:
first_group[["val"]] <- values1
first_group[["co"]] <- coeff1

second_group[["val"]] <- values2
second_group[["co"]] <- coeff2

third_group[["val"]] <- values3
third_group[["co"]] <- coeff3

fourth_group[["val"]] <- values4
fourth_group[["co"]] <- coeff4

#concatenate lists:
dummy_list <- list()

dummy_list[["first-group"]] <- first_group
dummy_list[["second-group"]] <- second_group
dummy_list[["third-group"]] <- third_group
dummy_list[["fourth-group"]] <- fourth_group

rm(values1, values2, values3, values4, coeff1, coeff2, coeff3, coeff4, first_group, second_group, third_group, fourth_group)
gc()

#show list
print(dummy_list)

I have a large nested list (list of named lists) - the example of such a list is given below. I would like to create a new list, in which only sub-lists with "co" vectors containing both 0 and 1 values would be preserved, while 0-only sublists would be discarded (eg. the output should contain only first-, third- and fourth- subgroups.
I played with lapply and filter according to this thread:

Subset elements in a list based on a logical condition

However, it throwed errors. I would appreciate tips how to handle lists within the lists.

# reprex
set.seed(123)

## empty lists
first_group <- list()
second_group <- list()
third_group <- list()
fourth_group <- list()

# dummy_vecs
values1 <- c(sample(120:730, 30, replace=TRUE))
coeff1 <- c(sample(0:1, 30, replace=TRUE))

values2 <- c(sample(50:810, 43, replace=TRUE))
coeff2 <- c(rep(0, 43))

values3 <- c(sample(510:730, 57, replace=TRUE))
coeff3 <- c(rep(0, 8), rep(1, 4), rep(0, 45))

values4 <- c(sample(123:770, 28, replace=TRUE))
coeff4 <- c(sample(0:1, 28, replace=TRUE))

## fill lists with values:
first_group[["val"]] <- values1
first_group[["co"]] <- coeff1

second_group[["val"]] <- values2
second_group[["co"]] <- coeff2

third_group[["val"]] <- values3
third_group[["co"]] <- coeff3

fourth_group[["val"]] <- values4
fourth_group[["co"]] <- coeff4

#concatenate lists:
dummy_list <- list()

dummy_list[["first-group"]] <- first_group
dummy_list[["second-group"]] <- second_group
dummy_list[["third-group"]] <- third_group
dummy_list[["fourth-group"]] <- fourth_group

rm(values1, values2, values3, values4, coeff1, coeff2, coeff3, coeff4, first_group, second_group, third_group, fourth_group)
gc()

#show list
print(dummy_list)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2025-01-24 12:06:23

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

first-group`

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

first-group`$val [1] 534 582 298 645 314 237 418 348 363 133 493 721 722 210 467 474 145 638 545 330 709 712 674 492 262 663 609 142 428 254

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

first-group`$co [1] 0 0 1 1 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 1 0 1 1 1 1 0

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

third-group`

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

third-group`$val [1] 713 721 683 526 699 555 563 672 619 603 588 533 622 724 616 644 730 716 660 663 611 669 644 664 679 514 579 525 533 541 530 564 584 673 592 726 548 563 727 [40] 646 708 557 586 592 693 620 548 705 510 677 539 603 726 525 597 563 712

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

third-group`$co [1] 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

fourth-group`

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

fourth-group`$val [1] 142 317 286 174 656 299 676 206 645 755 514 424 719 741 711 552 550 372 551 520 650 503 667 162 644 595 322 247

您可以从基本R中使用过滤器

Filter(function(x) sum(x$co) !=0, dummy_list)

或者可以使用 purrr

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

output

fourth-group`$co [1] 0 0 0 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1

但是,如果您还想排除任何 co <​​co /代码>具有所有 1 s,然后我们可以添加额外的条件。

Filter(function(x) sum(x$co) !=0 & sum(x$co == 0) > 0, dummy_list)

Purrr

dummy_list %>%
  keep( ~ sum(.$co) != 0 & sum(.$co == 0) > 0)

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

first-group`

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

first-group`$val [1] 534 582 298 645 314 237 418 348 363 133 493 721 722 210 467 474 145 638 545 330 709 712 674 492 262 663 609 142 428 254

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

first-group`$co [1] 0 0 1 1 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 1 0 1 1 1 1 0

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

third-group`

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

third-group`$val [1] 713 721 683 526 699 555 563 672 619 603 588 533 622 724 616 644 730 716 660 663 611 669 644 664 679 514 579 525 533 541 530 564 584 673 592 726 548 563 727 [40] 646 708 557 586 592 693 620 548 705 510 677 539 603 726 525 597 563 712

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

third-group`$co [1] 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

fourth-group`

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

fourth-group`$val [1] 142 317 286 174 656 299 676 206 645 755 514 424 719 741 711 552 550 372 551 520 650 503 667 162 644 595 322 247

You could use Filter from base R:

Filter(function(x) sum(x$co) !=0, dummy_list)

Or you can use purrr:

library(tidyverse)

dummy_list %>%
  keep( ~ sum(.$co) != 0)

Output

fourth-group`$co [1] 0 0 0 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1

However, if you also want to exclude any co that have all 1s, then we can add an extra condition.

Filter(function(x) sum(x$co) !=0 & sum(x$co == 0) > 0, dummy_list)

purrr

dummy_list %>%
  keep( ~ sum(.$co) != 0 & sum(.$co == 0) > 0)
迷乱花海 2025-01-24 12:06:23
# create boolean for where condition is TRUE
cond <- sapply(dummy_list, function(x) any(0 %in% x$co) & any(1 %in% x$co))

# subset
dummy_list[cond]
# create boolean for where condition is TRUE
cond <- sapply(dummy_list, function(x) any(0 %in% x$co) & any(1 %in% x$co))

# subset
dummy_list[cond]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文