0

I want to use the data of [x] to fill in [test] based on certain sequence:

x = matrix(rnorm(330),165,2)


origins = 130:157

horizon = 8

col = 1:2

test = array(0, c(length(origins)*length(col), horizon))

for( origin in origins){

  for (c in col){

    test[which(origin==origins), ] = x[(origin+1):(origin+8), c] 

  }

}

However, this code only helps extract the second column of [x] to fill in the first 28 rows of [test]. The following picture is only a part of a complete [test] table, showing the ineffective filling from row 29 to row 56. enter image description here

Anyone who can help me fill in them completely? Thank you very much.

  • Your third line has a spelling error (missing letter i). tPlease provide some sample data and what your result should like like. Take a look at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Andrew Lavers Aug 05 '17 at 22:39
  • I have uploaded the wrong result which is in the attached picture. Thank you for information. – Wang Yukun Aug 06 '17 at 00:01

1 Answers1

0

Here is a possible solution, but it is still not clear what you want the result to be. better to make much smaller data and show desired result.

The left hand side of the assignment, in the original code, does not vary with c, so each time through the loop for c the same rows of test will be overwrittem,

x = matrix(rnorm(330),165,2)

origins = 130:157
horizon = 8
col = 1:2

test = array(0, c(length(origins)*length(col), horizon))

for( origin in origins){

  for (c in col){

    # the left hand side must vary somehow with c as well.
    test[(which(origin==origins)-1) + (c - 1) * length(origins) + 1, ] = x[(origin+1):(origin+8), c] 

  }

}
Andrew Lavers
  • 4,328
  • 1
  • 12
  • 19
  • Thank you for your help. It almost helps me. The result for your code is that the observations 131-138 in the 1st column of [x] are filled in the 1st row of [test], then the observations 131-138 in the 2nd column of [x] are filled in the 2nd row of [test], then successively. – Wang Yukun Aug 06 '17 at 01:38
  • It's better if you can help to achieve: the observations 131-138 in the 1st column of [x] are filled in the 1st row of [test], the observations 132-138 in the 1st column of [x] are filled in the 2nd row of [test],..., after 28 loops, the observations 131-138 in the 2nd column of [x] are filled in the 29th row of [test], the observations 132-139 in the 2nd column of [x] are filled in the 30th row of [test],...,successively – Wang Yukun Aug 06 '17 at 01:40
  • It seems correct to me based on your description. But try use `x = matrix(c(seq(1.1, 165.1, 1),seq(1.2, 165.2, 1)),165,2)` as test data. If not make the test data much much smaller (just a few rows) and show (give example) of the desired output. You will get much better answers if you show expected results! – Andrew Lavers Aug 06 '17 at 02:27
  • Yeah, you are correct! Thank you very very much. The last question, if the length of "col" is changed, is there any difference for coding? Subsequently, I would make col = 1:18, then col =1:145, and so on. The length of "col" would become longer and longer, does it affect coding? – Wang Yukun Aug 06 '17 at 10:42
  • it will work for any value of `col`. You should also fix `+8' to be `+horizon` Please accept as the answer. – Andrew Lavers Aug 06 '17 at 10:50
  • Okay. You really help me a lot. I will try. Thank you. Have a good day! – Wang Yukun Aug 06 '17 at 11:44