Tou Mou
Tou Mou

Reputation: 1274

map a vector into list based on a size_vector

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

Upvotes: 0

Views: 40

Answers (1)

Sotos
Sotos

Reputation: 51582

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

Upvotes: 3

Related Questions