Converts all coordinates from various geometric shapes into a single data.frame.

gm_coordinates(x)

Arguments

x

object representing geometry shapes (e.g., list of matrices)

Value

a single data.frame representing all the values in the input lists and matrices.

Details

The data.frame returned will always have an 'id' column. Then will follow an 'id+counter' column for every level of nesting the geometry is within.

The coordinates always start in column 'c1', the first column after all the id columns. Then there is a column 'c+counter' for every coordinate in the geometry.

This function is designed to handle multiple and different nested of geometry structures.

Examples


x <- 1:3
gm_coordinates( x )
#>   id c1 c2 c3
#> 1  1  1  2  3

m <- matrix(1:12, ncol = 3)
gm_coordinates( m )
#>   id c1 c2 c3
#> 1  1  1  5  9
#> 2  1  2  6 10
#> 3  1  3  7 11
#> 4  1  4  8 12

l <- list(
  matrix(1:12, ncol = 2 )
)
gm_coordinates( l )
#>   id id1 c1 c2
#> 1  1   1  1  7
#> 2  1   1  2  8
#> 3  1   1  3  9
#> 4  1   1  4 10
#> 5  1   1  5 11
#> 6  1   1  6 12

l <- list(
  matrix(1:12, ncol = 4 )
)
gm_coordinates( l )
#>   id id1 c1 c2 c3 c4
#> 1  1   1  1  4  7 10
#> 2  1   1  2  5  8 11
#> 3  1   1  3  6  9 12

l <- list(
  list(
    matrix(1:12, ncol = 2)
  )
)
gm_coordinates( l )
#>   id id1 id2 c1 c2
#> 1  1   1   1  1  7
#> 2  1   1   1  2  8
#> 3  1   1   1  3  9
#> 4  1   1   1  4 10
#> 5  1   1   1  5 11
#> 6  1   1   1  6 12

l <- list(
  list(
    matrix(1:12, ncol = 2)
    , matrix(1:4, ncol = 2)
  )
)
gm_coordinates( l )
#>   id id1 id2 c1 c2
#> 1  1   1   1  1  7
#> 2  1   1   1  2  8
#> 3  1   1   1  3  9
#> 4  1   1   1  4 10
#> 5  1   1   1  5 11
#> 6  1   1   1  6 12
#> 7  1   2   2  1  3
#> 8  1   2   2  2  4

l <- list(
  list(
    matrix(1:12, ncol = 2)
    , matrix(1:4, ncol = 2)
  )
  , 1:5
  , 1:2
  , matrix(1:9, ncol = 3)
)
gm_coordinates( l )
#>    id id1 id2 c1 c2 c3 c4 c5
#> 1   1   1   1  1  7 NA NA NA
#> 2   1   1   1  2  8 NA NA NA
#> 3   1   1   1  3  9 NA NA NA
#> 4   1   1   1  4 10 NA NA NA
#> 5   1   1   1  5 11 NA NA NA
#> 6   1   1   1  6 12 NA NA NA
#> 7   1   2   2  1  3 NA NA NA
#> 8   1   2   2  2  4 NA NA NA
#> 9   2  NA  NA  1  2  3  4  5
#> 10  3  NA  NA  1  2 NA NA NA
#> 11  4  NA   1  1  4  7 NA NA
#> 12  4  NA   1  2  5  8 NA NA
#> 13  4  NA   1  3  6  9 NA NA

l <- list(
  matrix(1:4, ncol = 2)
  , list(
    matrix(1:9, ncol = 3)
  )
)
gm_coordinates( l )
#>   id id1 id2 c1 c2 c3
#> 1  1  NA   1  1  3 NA
#> 2  1  NA   1  2  4 NA
#> 3  2   1   1  1  4  7
#> 4  2   1   1  2  5  8
#> 5  2   1   1  3  6  9

l <- list(
  list(
    list(
      matrix(1:12, ncol = 2)
    )
  )
  , list(
    list(
     matrix(1:24, ncol = 2)
   )
  )
)
gm_coordinates( l )
#>    id id1 id2 id3 c1 c2
#> 1   1   1   1   1  1  7
#> 2   1   1   1   1  2  8
#> 3   1   1   1   1  3  9
#> 4   1   1   1   1  4 10
#> 5   1   1   1   1  5 11
#> 6   1   1   1   1  6 12
#> 7   2   1   1   1  1 13
#> 8   2   1   1   1  2 14
#> 9   2   1   1   1  3 15
#> 10  2   1   1   1  4 16
#> 11  2   1   1   1  5 17
#> 12  2   1   1   1  6 18
#> 13  2   1   1   1  7 19
#> 14  2   1   1   1  8 20
#> 15  2   1   1   1  9 21
#> 16  2   1   1   1 10 22
#> 17  2   1   1   1 11 23
#> 18  2   1   1   1 12 24

l <- list(
  list(
    list(
      matrix(1:12, ncol = 2)
    )
  )
  , list(
    list(
      matrix(1:3, ncol = 3)
     , matrix(1:24, ncol = 2)
   )
  )
)
gm_coordinates( l )
#>    id id1 id2 id3 c1 c2 c3
#> 1   1   1   1   1  1  7 NA
#> 2   1   1   1   1  2  8 NA
#> 3   1   1   1   1  3  9 NA
#> 4   1   1   1   1  4 10 NA
#> 5   1   1   1   1  5 11 NA
#> 6   1   1   1   1  6 12 NA
#> 7   2   1   1   1  1  2  3
#> 8   2   1   2   2  1 13 NA
#> 9   2   1   2   2  2 14 NA
#> 10  2   1   2   2  3 15 NA
#> 11  2   1   2   2  4 16 NA
#> 12  2   1   2   2  5 17 NA
#> 13  2   1   2   2  6 18 NA
#> 14  2   1   2   2  7 19 NA
#> 15  2   1   2   2  8 20 NA
#> 16  2   1   2   2  9 21 NA
#> 17  2   1   2   2 10 22 NA
#> 18  2   1   2   2 11 23 NA
#> 19  2   1   2   2 12 24 NA