'Merge 2d coordinate array into 1d character list in R

I have a large array with 118420 elements. The array is numeric, and the dimensions are below

dim(Watershed_Coords)
[1]     1 59210     2

The problem is that the values in this array are coordinates where [lat] and [lon] have been split. I would like to merge these to have a single character list in the form of "[lon,lat]" where the , is also included.



Solution 1:[1]

You can do:

lat_lon <- paste0("[", Watershed_Coords[,,1], ", ", Watershed_Coords[,,2], "]")

And to demonstrate this is the correct, format, we can do:

head(data.frame(lat_lon))
#>        lat_lon
#> 1 [0.74, 0.21]
#> 2 [0.93, 0.34]
#> 3 [0.58, 0.54]
#> 4  [0.8, 0.57]
#> 5 [0.39, 0.94]
#> 6 [0.64, 0.06]

Created on 2022-03-02 by the reprex package (v2.0.1)


Data

Watershed_Coords <- round(runif(59210 * 2), 2)
dim(Watershed_Coords) <- c(1, 59210, 2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Allan Cameron