0

I have a list of 100 items. I want to split it after each 10th item in Code 1. Code 2 is about a list of two former lists and splitting it to 20 lists of 10 items each.

Code 1

Expected output: ten lists of 10 items.

A <- 100
a <- rnorm(A) # [1:100]
n <- 10
str(a)

# Not resulting in equal size of chunks with vectors so reject
# http://stackoverflow.com/a/3321659/54964
#d <- split(d, ceiling(seq_along(d)/(length(d)/n)))

# Works for vectors but not with lists
# http://stackoverflow.com/a/16275428/54964
#d <- function(d,n) split(d, cut(seq_along(d), n, labels = FALSE)) 

str(d)

Test code 2

Input: a list of two lists

aa <- list(a, rnorm(a))

Expected output: 20 lists of 10 item size

Testing Loki's answer

segmentLists <- function(A, segmentSize) {
  res <- lapply(A, function(x) split(unlist(x), cut(seq_along(unlist(x)), segmentSize, labels = F)))

  #print(res)    
  res <- unlist(res, recursive = F)
}

segmentLists(aa, 10)

Output: loop going on, never stopping

OS: Debian 8.5
R: 3.3.1

smci
  • 32,567
  • 20
  • 113
  • 146
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

1 Answers1

1

you can use lapply.

aa <- list(a, rnorm(a))
aa
n <- 10

x <- lapply(aa, function(x) split(unlist(x), cut(seq_along(unlist(x)), n, labels = F)))
y <- unlist(x, recursive = F)
str(y)
# List of 20
# $ 1 : num [1:10] 1.0895 -0.0477 0.225 -0.6308 -0.1558 ...
# $ 2 : num [1:10] -0.469 -0.381 0.709 -0.798 1.183 ...
# $ 3 : num [1:10] 0.757 -1.128 -1.394 -0.712 0.494 ...
# $ 4 : num [1:10] 1.135 0.324 0.75 -0.83 0.794 ...
# $ 5 : num [1:10] -0.786 -0.068 -0.179 0.354 -0.597 ...
# $ 6 : num [1:10] -0.115 0.164 -0.365 -1.827 -2.036 ...
...

length(y)
# [1] 20

to remove the names of the list elements in y ($ 1, $ 2 etc.) you can use unname()

str(unname(y))
# List of 20
# $ : num [1:10] 1.0895 -0.0477 0.225 -0.6308 -0.1558 ...
# $ : num [1:10] -0.469 -0.381 0.709 -0.798 1.183 ...
# $ : num [1:10] 0.757 -1.128 -1.394 -0.712 0.494 ...
# $ : num [1:10] 1.135 0.324 0.75 -0.83 0.794 ...
# $ : num [1:10] -0.786 -0.068 -0.179 0.354 -0.597 ...
...

Using a function, you have to return res at the end of the function.

segmentLists <- function(A, segmentSize)
{
  res <- lapply(A, function(x) split(unlist(x), cut(seq_along(unlist(x)), segmentSize, labels = F)))

  #print(res)

  res <- unlist(res, recursive = F)
  res <- unname(res)
  res
}
loki
  • 9,816
  • 7
  • 56
  • 82
  • 1
    @DavidArenburg, that's correct. I changed it. @Masi: i added `unlist(...)` that solves the problem. See the result of `length(...)`. – loki Nov 06 '16 at 20:28
  • 1
    These are names of the list elements. See the edit how to remove them. – loki Nov 06 '16 at 20:43
  • 1
    The Function doesn't loop, but you don't return any objects. You have to return the result `res` at the end of the function. See the edit – loki Nov 06 '16 at 21:16
  • I don't think there is a matrix of lists, am I wrong? However, you could try to vectorize the matrix into a list of 4 lists. Then, the function can be applied. – loki Nov 06 '16 at 21:57
  • 1
    I think this would exceed the answer to your question, which is aleardy answered. [This post](http://stackoverflow.com/a/6821395/3250126) answers this, I think. – loki Nov 06 '16 at 22:10
  • @Masi you probably should edit your question and remove the functions which is missing the return value. otherwise, future users could be confused – loki Nov 15 '16 at 13:06