Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a json array from R that has a sequence of lat and long?

Tags:

json

r

How do I write a json array from R that has a sequence of lat and long?

I would like to write:

[[[1,2],[3,4],[5,6]]]

the best I can do is:

toJSON(matrix(1:6, ncol = 2, byrow = T))
#"[ [ 1, 2 ],\n[ 3, 4 ],\n[ 5, 6 ] ]"

How can I wrap the thing in another array (the json kind)? This is important to me so I can write files into a geojson format as a LineString.

like image 451
cylondude Avatar asked Oct 09 '14 06:10

cylondude


1 Answers

I usually use fromJSON to get the target object :

ll <- fromJSON('[[[1,2],[3,4],[5,6]]]')

str(ll)
List of 1
 $ :List of 3
  ..$ : num [1:2] 1 2
  ..$ : num [1:2] 3 4
  ..$ : num [1:2] 5 6

So we should create , a list of unnamed list, each containing 2 elements:

 xx <- list(setNames(split(1:6,rep(1:3,each=2)),NULL))
identical(toJSON(xx),'[[[1,2],[3,4],[5,6]]]')
[1] TRUE
like image 169
agstudy Avatar answered Oct 11 '22 17:10

agstudy