Question

I have a list where each slice is of unequal lengths. Also, the number of slices is not known in advance, but I would like to set each slice to the max size. Below is an example, where the largest length of both slices is 20. The second slice has a length of 6, so I want to convert that to a length of 20, too. Preferably, the new length is filled with NA's or NULL.

data <- c(1,12,2,43,1,23,6,34,1,3,4,7,9,67,5,2,1,3,4,5,6,78,9,99,100,0)
splitData <- split(data, ceiling(seq_along(data)/20))

Thank you!

Était-ce utile?

La solution

You can make use of the [ operator, which when accessed beyond the length of a vector returns NA. You can combine this with lapply as follows:

lapply(splitData, `[`, 1:20)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top