Pierre L
Pierre L

Reputation: 28441

R Creating list of divided vector by index

With a code like this example:

set.seed(11)
x <- sample(letters)
x
 [1] "h" "a" "m" "y" "b" "u" "v" "f" "p" "c" "q" "g" "x" "l" "i" "o" "e" "r" "t" "d" "z" "k" "s" "j" "w" "n"

And provided with this vector of values

y <- c(4, 13, 20)

I'd like to split the x vector up by the y vector as a 'slicing' index. And to group the result as a list. Desired output:

z <- list(c("h", "a", "m", "y"),c("b", "u", "v", "f", "p", "c", "q", "g", "x"), c("l", "i", "o", "e", "r", "t", "d"), c("z", "k", "s", "j", "w", "n") )
z
[[1]]
[1] "h" "a" "m" "y"

[[2]]
[1] "b" "u" "v" "f" "p" "c" "q" "g" "x"

[[3]]
[1] "l" "i" "o" "e" "r" "t" "d"

[[4]]
[1] "z" "k" "s" "j" "w" "n"

Upvotes: 2

Views: 110

Answers (2)

akrun
akrun

Reputation: 887078

We can either create a vector of '0s' equal to the length of 'x', use y as a numeric index to replace the elements in v1 with 1, cumsum the result and use that as grouping vector to split 'x'

v1 <- numeric(length(x))
v1[y+1] <- 1
split(x,cumsum(v1))

Or we can get the grouping vector by doing cumsum on the results from tabulate

 split(x,cumsum(tabulate(y+1, length(x))))

Or use match

split(x,cumsum(c(TRUE,!is.na(match(seq_along(x), y)[-length(x)]))))

Or %in%

 split(x,cumsum(seq_along(x) %in% (y+1)))

Upvotes: 3

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

For fun, another way to create the splitting vector could be to use cut:

split(x, cut(seq_along(x), c(-Inf, y, Inf)))
# $`(-Inf,4]`
# [1] "h" "a" "m" "y"
# 
# $`(4,13]`
# [1] "b" "u" "v" "f" "p" "c" "q" "g" "x"
# 
# $`(13,20]`
# [1] "l" "i" "o" "e" "r" "t" "d"
# 
# $`(20, Inf]`
# [1] "z" "k" "s" "j" "w" "n"

It even tells you which group the data belong in :-)


By extension, that also means that findInterval would work:

split(x, findInterval(seq_along(x), y+1))

In both cases, we are looking at which bins the values from 1 to the length of the input vector "x" fall into, where the endpoints are defined by "y".

Upvotes: 3

Related Questions