'R transpose 2 matrices into a list of tibbles (for a nested df)
I have two matrices, of latitude and longitude, both of which are 50 column x 1 million (e.g.) rows. I need to create a list of 1 million tibbles, each 2 columns - lon and lat - and 50 rows. My current code is:
lonlat <- list()
for (i in 1:nrow(lon)) {
lonlat[[i]] <- tibble(lon = lon[i, ], lat = lat[i, ])
}
I'm aware that this is incredibly inefficient, but I can't get my head around how I'd do this with purrr. I feel like map2 could be the answer, but I suspect I'm not thinking about this the right way, and possibly I should reorganise the input matrices in order to make it a simpler task.
Does anyone have any experience with purrr/map2, or this kind of problem? Thanks in advance for any ideas.
Solution 1:[1]
Here is an option using asplit + array (borrow data from @r2evans)
> asplit(array(cbind(lat, lon), c(dim(lat), 2)), 1)
[[1]]
[,1] [,2]
[1,] 1 51
[2,] 5 55
[3,] 9 59
[4,] 13 63
[5,] 17 67
[[2]]
[,1] [,2]
[1,] 2 52
[2,] 6 56
[3,] 10 60
[4,] 14 64
[5,] 18 68
[[3]]
[,1] [,2]
[1,] 3 53
[2,] 7 57
[3,] 11 61
[4,] 15 65
[5,] 19 69
[[4]]
[,1] [,2]
[1,] 4 54
[2,] 8 58
[3,] 12 62
[4,] 16 66
[5,] 20 70
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ThomasIsCoding |
