0

I've seen this answer to take a list of numbers and apply a function to each element to make a modified list: https://stackoverflow.com/a/26164349/115237

Given a list like this:

# x <- sample(seq(1, 91, 10))
x <- c(1, 61, 81, 51, 41, 71, 31, 21, 11, 91)

How can I apply a function / use a list-comprehension to take each element and then "add-in" the next 9 numbers, returning this for the above list:

c(1:10, 61:70, 81:90, 51:60, 41:50, 71:80, 31:40, 21:30, 11:20, 91:100)

I'm using this to roll my own sampling to get chunks of ten at a time: the first ten, then 60s, then 80s, ... . I've been trying to use a function like this:

f1 <- function(x) {seq(x,x+10)}

Also, is there a built-in tool for this kind of sampling - where you can also do replacement in the sampling?

Community
  • 1
  • 1
Noel Evans
  • 8,113
  • 8
  • 48
  • 58

2 Answers2

3

You can use

as.vector(mapply(seq, from = x, to = x+9))
lukeA
  • 53,097
  • 5
  • 97
  • 100
0

Or you could do

 c(matrix(x+rep(0:9,each=length(x)),ncol=10, byrow=TRUE))
akrun
  • 874,273
  • 37
  • 540
  • 662