0

I need to build a list of coordinates from two variables, but I can't figure out how to do this in R

This data contains 2 variables lat and long

data_table <- structure(list(lat = c(1.2, 1.54),
                     long = c(4.23, 4.29)),
                row.names = c(NA,-2L), class = c("data.table", "data.frame"))

lat   long
---------
1.2   4.23
1.54  4.29

and I would like to constitute a couple of data in a list of vectors, to obtain the following structure:

 list(
      c(1.2, 4.23),
      c(1.54, 4.29)
    )

Expected output:

[[1]]
[1]  1.2 4.23

[[2]]
[1]  1.54 4.29
Daoudi Karim
  • 109
  • 7

2 Answers2

3

You can use asplit:

asplit(data_table, 1)

output

# [[1]]
#  lat long 
# 1.20 4.23 
# 
# [[2]]
#  lat long 
# 1.54 4.29 
Maël
  • 45,206
  • 3
  • 29
  • 67
2

One

> as.list(as.data.frame(t(data_table)))
$V1
[1] 1.20 4.23

$V2
[1] 1.54 4.29
user2974951
  • 9,535
  • 1
  • 17
  • 24
  • 1
    Assuming OP has `data.table` loaded already you can replace `as.data.frame(t(data_table))` with more efficient `transpose(data_table)`. – s_baldur Dec 12 '22 at 11:00