matrix - combine each item from one list with each item of another in r -
i trying combine 2 vectors of sequential numbers in order produce matrix can plot grid. vectors contain longitude , latitude data respectively, simplicity sake, lets have:
matrix(seq(1, 3, 1)) [,1] [1,] 1 [2,] 2 [3,] 3
and
matrix(seq(1, 3, 1)) [,1] [1,] 1 [2,] 2 [3,] 3
, want is:
[,1] [,2] [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 2 1 [5,] 2 2 [6,] 2 3 [7,] 3 1 [8,] 3 2 [9,] 3 3
(the above fake , not r) i've tried using nested loop no avail. seems way simple giving me such trouble.
you seem looking expand.grid()
a <- 1:3 b <- 1:3 expand.grid(a=a,b=b) # b # 1 1 1 # 2 2 1 # 3 3 1 # 4 1 2 # 5 2 2 # 6 3 2 # 7 1 3 # 8 2 3 # 9 3 3 ## or perhaps, if order matters rev(expand.grid(b=b,a=a)) # b # 1 1 1 # 2 1 2 # 3 1 3 # 4 2 1 # 5 2 2 # 6 2 3 # 7 3 1 # 8 3 2 # 9 3 3
Comments
Post a Comment