R:将相同长度的列表组合成元组列表

R: Combine lists of same length into list of tupels

我目前正在努力解决 R 中的以下问题: 给定两个列表 a=(a1,...,an) 和 b=(b1,...,bn),我想得到一个列表列表,例如 ((a1,b1),...,(an,十亿))。 背景信息:我需要将纯数据 table/frame 转换为嵌套数据 json(例如使用 jsonlite)。 作为一个代码示例,考虑一个数据 table,其中包含 ID、纬度和经度,后两者汇总在一个名为 location:

的列表中
library(data.table)
n<-5
data<-data.table(id=1:n,lon=1:n,lat=1:n)

在这里,我们可以使用lapply来获得所需的结果:

data$location<-lapply(1:nrow(data),function(x) list(data[x,c("lat","lon"),with=F]) )

交替拆分(小数据集更快,大数据集更慢):

data$location<-list(split(data[,c("lat","lon"),with=F],1:nrow(data)))

两者在小规模上都工作得很好,但对于 n>>10^5,它在我的机器上需要很长时间。您有什么解决方案可以加快计算速度吗?

我们可以在 'data.table' 中按 'id' 分组后将 Data.table 的子集放在 list 中,并提取默认的新列 'V1'

data[, location := list(list(.SD)), id]

我们可以提取 list

data$location
#[[1]]
#   lon lat
#1:   1   1

#[[2]]
#   lon lat
#1:   2   2

#[[3]]
#   lon lat
#1:   3   3

#[[4]]
#   lon lat
#1:   4   4

#[[5]]
#   lon lat
#1:   5   5

您可以使用 data.table::transpose 作为另一个选项:

data[, location := transpose(.(lon, lat))]

data
#   id lon lat location
#1:  1   1   1      1,1
#2:  2   2   2      2,2
#3:  3   3   3      3,3
#4:  4   4   4      4,4
#5:  5   5   5      5,5

data$location        # this drops the column names, you can refer to the elements by index
#[[1]]
#[1] 1 1

#[[2]]
#[1] 2 2

#[[3]]
#[1] 3 3

#[[4]]
#[1] 4 4

#[[5]]
#[1] 5 5