Home > front end >  map a vector into list based on a size_vector
map a vector into list based on a size_vector

Time:10-07

Assume we have the following vector :

size_1 = c(3,4,2,3,2,1)

dput(as.numeric(row.names(strata(tmp[,-ncol(tmp)], "Defect_type", size = size_1, method = "srswor"))))

output=c(8, 18, 19, 22, 23, 26, 36, 51, 52, 69, 72, 78, 89, 94, 105)

I need to map output into a list with length=length(size_1):

list(c(8, 18, 19),c(22, 23, 26, 36),c(51, 52),c(69, 72, 78),c(89, 94),c(105))
[[1]]
[1]  8 18 19

[[2]]
[1] 22 23 26 36

[[3]]
[1] 51 52

[[4]]
[1] 69 72 78

[[5]]
[1] 89 94

[[6]]
[1] 105

CodePudding user response:

You can do,

split(output, rep(seq(size_1), size_1))

$`1`
[1]  8 18 19

$`2`
[1] 22 23 26 36

$`3`
[1] 51 52

$`4`
[1] 69 72 78

$`5`
[1] 89 94

$`6`
[1] 105
  •  Tags:  
  • r
  • Related