p<-c("a","b","c")
x<-c("41.434931824218786","41.42672694456612","41.647157029217425")
y<-c("2.1759165563219116","2.1954430374270455","1.4942063178224638")
df<-data.frame(p,x,y)
I want to convert these coordinates into UTMX y UTMY.
Another solution is to use directly st_as_sf
indicating the columns with the x and y coordinates, as well as its crs. Also, if you wish to retain the other columns info (e.g., p column) you can use mutate
to add the coordinates as new columns. Finally, if you are no longer interested in the geometry column, you can remove it using st_drop_geometry
.
library(sf)
library(dplyr)
df |>
# Transform df from data frame to sf object
st_as_sf(coords = c("x", "y"),
crs = 4326) |>
# Reproject data to new crs
st_transform(23031) |>
# Obtain coordinates as new columns
mutate(coords = st_coordinates(geometry)) |>
# Drop geometry column
st_drop_geometry()
# p coords.X coords.Y
#1 a 5137810.8 307601.4
#2 b 5136560.9 310324.5
#3 c 5170504.7 211895.1