Reputation: 13
I'm currently struggling with the following problem in R: Given two lists a=(a1,...,an) and b=(b1,...,bn), I want to get a list of lists like ((a1,b1),...,(an,bn)). Background info: I need to convert a plain data table/frame into a nested json (e.g. using jsonlite). As an code example consider a data table with ids, latitudes and longitudes, the latter two to be summarized in a list called location:
library(data.table)
n<-5
data<-data.table(id=1:n,lon=1:n,lat=1:n)
Here, we could use lapply to get the required result:
data$location<-lapply(1:nrow(data),function(x) list(data[x,c("lat","lon"),with=F]) )
Alternatively split (faster for small, slower for bigger data sets):
data$location<-list(split(data[,c("lat","lon"),with=F],1:nrow(data)))
Both work fine on small scale, but for n>>10^5 it takes ages on my machine. Do you have any solution in mind for faster computation?
Upvotes: 1
Views: 613
Reputation: 887541
We can place the Subset of Data.table in a list
after grouping by 'id' in 'data.table' and extract the default new column 'V1'
data[, location := list(list(.SD)), id]
We can extract the list
column
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
Upvotes: 1
Reputation: 215057
You can use data.table::transpose
as another option:
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
Upvotes: 0