R 将 2 个矩阵转置为 tibbles 列表(对于嵌套 df)

发布于 2025-01-10 22:15:20 字数 437 浏览 0 评论 0原文

我有两个矩阵,纬度和经度,两者都是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见你 2025-01-17 22:15:20

你的“50栏”在这里是5;你的“100万行”在这里是4。

lat <- matrix(1:20, nr=4)
lon <- matrix(50 + 1:20, nr=4)
lat
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    5    9   13   17
# [2,]    2    6   10   14   18
# [3,]    3    7   11   15   19
# [4,]    4    8   12   16   20
lon
#      [,1] [,2] [,3] [,4] [,5]
# [1,]   51   55   59   63   67
# [2,]   52   56   60   64   68
# [3,]   53   57   61   65   69
# [4,]   54   58   62   66   70

你的 100 万长列表在这里是 4 长的,每个都有 2 列和 5 行。

Map(tibble, lat=asplit(lat, 1), lon=asplit(lon, 1))
# [[1]]
# # A tibble: 5 x 2
#     lat   lon
#   <int> <dbl>
# 1     1    51
# 2     5    55
# 3     9    59
# 4    13    63
# 5    17    67
# [[2]]
# # A tibble: 5 x 2
#     lat   lon
#   <int> <dbl>
# 1     2    52
# 2     6    56
# 3    10    60
# 4    14    64
# 5    18    68
# [[3]]
# # A tibble: 5 x 2
#     lat   lon
#   <int> <dbl>
# 1     3    53
# 2     7    57
# 3    11    61
# 4    15    65
# 5    19    69
# [[4]]
# # A tibble: 5 x 2
#     lat   lon
#   <int> <dbl>
# 1     4    54
# 2     8    58
# 3    12    62
# 4    16    66
# 5    20    70

如果你真的想使用purrr,那么

purrr::map2(asplit(lat, 1), asplit(lon, 1), ~ tibble(lat=.x, lon=.y))

Your "50 columns" is 5 here; your "1 million rows" is 4 here.

lat <- matrix(1:20, nr=4)
lon <- matrix(50 + 1:20, nr=4)
lat
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    5    9   13   17
# [2,]    2    6   10   14   18
# [3,]    3    7   11   15   19
# [4,]    4    8   12   16   20
lon
#      [,1] [,2] [,3] [,4] [,5]
# [1,]   51   55   59   63   67
# [2,]   52   56   60   64   68
# [3,]   53   57   61   65   69
# [4,]   54   58   62   66   70

There your 1-million-long list is 4-long here, each with 2 columns and 5 rows.

Map(tibble, lat=asplit(lat, 1), lon=asplit(lon, 1))
# [[1]]
# # A tibble: 5 x 2
#     lat   lon
#   <int> <dbl>
# 1     1    51
# 2     5    55
# 3     9    59
# 4    13    63
# 5    17    67
# [[2]]
# # A tibble: 5 x 2
#     lat   lon
#   <int> <dbl>
# 1     2    52
# 2     6    56
# 3    10    60
# 4    14    64
# 5    18    68
# [[3]]
# # A tibble: 5 x 2
#     lat   lon
#   <int> <dbl>
# 1     3    53
# 2     7    57
# 3    11    61
# 4    15    65
# 5    19    69
# [[4]]
# # A tibble: 5 x 2
#     lat   lon
#   <int> <dbl>
# 1     4    54
# 2     8    58
# 3    12    62
# 4    16    66
# 5    20    70

If you really want to use purrr, then

purrr::map2(asplit(lat, 1), asplit(lon, 1), ~ tibble(lat=.x, lon=.y))
还不是爱你 2025-01-17 22:15:20

这是使用 asplit + array 的选项(从 @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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文