0

I am quite new to R and wrote a double for-loop that does exactly what I want (a block-bootstrap of index numbers with a certain block length). However, I am unable to store in a matrix what print(x_sample) shows me. I have tried lapply and everything else I could find here, but I was unable to solve the problem by myself. I really hope that you can help. Many thanks in advance

Here is my code: x <- c(1:1060,1,2,3) x_sample <- numeric(119*4) for (i in 1:3){ for (j in 0:119){ idx <- sample(1:1060,1,replace=TRUE) x_sample[(4*j+1):(4*j+4)] <- x[(idx):(idx+3)] } print(x_sample)} 
1
  • Please accept the answer which helped you the most :) There will be a checkmark to the left of the answer--click it to accept that answer. Commented Jul 8, 2012 at 6:19

2 Answers 2

2

Do you mean something like this?:

values <- c(1:1060, 1, 2, 3) m <- sapply(sample(1:1060, 360, replace=TRUE), function(idx)values[(idx):(idx+3)]) 
Sign up to request clarification or add additional context in comments.

Comments

1

Create a matrix before the loop, and then fill the matrix:

x <- c(1:1060,1,2,3) mat <- matrix(nrow=480, ncol = 3) for (i in 1:3){ for (j in 0:119){ idx <- sample(1:1060,1,replace=TRUE) mat[(4*j+1):(4*j+4), i] <- x[(idx):(idx+3)] } print(mat)} 

1 Comment

thank you EDi! The solution was much easier than I expected. Guess I sill need to practice...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.