R 将 2 个矩阵转置为 tibbles 列表(对于嵌套 df)
我有两个矩阵,纬度和经度,两者都是 50 列 x 100 万(例如)行。我需要创建一个包含 100 万个 tibble 的列表,每列 2 列(经度和纬度)和 50 行。我当前的代码是:
lonlat <- list()
for (i in 1:nrow(lon)) {
lonlat[[i]] <- tibble(lon = lon[i, ], lat = lat[i, ])
}
我知道这是极其低效的,但我无法理解如何使用 purrr
来做到这一点。我觉得 map2
可能是答案,但我怀疑我没有以正确的方式思考这个问题,也许我应该重新组织输入矩阵以使其成为一个更简单的任务。
有没有人有过 purrr
/map2
或此类问题的经验?预先感谢您的任何想法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的“50栏”在这里是5;你的“100万行”在这里是4。
你的 100 万长列表在这里是 4 长的,每个都有 2 列和 5 行。
如果你真的想使用
purrr
,那么Your "50 columns" is 5 here; your "1 million rows" is 4 here.
There your 1-million-long list is 4-long here, each with 2 columns and 5 rows.
If you really want to use
purrr
, then这是使用
asplit
+array
的选项(从 @r2evans 借用数据一>)Here is an option using
asplit
+array
(borrow data from @r2evans)