gpt4 book ai didi

r - 如何在向量列表中对列表进行分组

转载 作者:行者123 更新时间:2023-12-01 13:47:14 24 4
gpt4 key购买 nike

我有一个包含 7 个向量的列表,我想将这些列表分成 4 个向量列表。

B2 <- list ( c (12 , 47 ,137 ,170), c(44 , 47 ,135, 170) , c(12 , 28 , 34 , 44 , 47 , 59 , 61 , 67 , 76 , 80 , 84 ,135, 148, 170) , c(44 , 47 , 84 ,135 ,170) , c(12 , 28 , 34 , 44 , 47 , 59 , 61 , 67 , 76 , 80 , 84 ,135, 148, 156, 159, 164, 170) , c(12 , 28 , 34 , 44 , 47 , 84 ,135 ,170) , c(12 , 28 , 44 , 47 , 84, 135, 170)) 

# I would like to create four groups (lists) as per the following index of consecutive list sequence)

Subgroup <- c(2,4,5,7)

# Desired Output

B2 <- list ( list ( c (12 , 47 ,137 ,170), c(44 , 47 ,135, 170)) , list ( c(12 , 28 , 34 , 44 , 47 , 59 , 61 , 67 , 76 , 80 , 84 ,135, 148, 170) , c(44 , 47 , 84 ,135 ,170)) , list ( c(12 , 28 , 34 , 44 , 47 , 59 , 61 , 67 , 76 , 80 , 84 ,135, 148, 156, 159, 164, 170)) , list ( c (12 , 28 , 34 , 44 , 47 , 84 ,135 ,170) , c(12 , 28 , 44 , 47 , 84, 135, 170)))

最佳答案

使用splitrep:

split(B2, rep(1:length(Subgroup), diff(c(0, Subgroup))))

关于r - 如何在向量列表中对列表进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35039329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com