read_csv saves geometry point data as character

Hello everyone! I have a seemingly basic problem I can't figure out.

I have a csv file and one of the columns contains geometric point data. All the values in the column are formatted like:

POINT (-12.345 67.8910)
POINT (-12.543 67.0198)

I'm using read_csv to import the dataset, and it keeps assigning the column type as character. I eventually want to plot the data using sf, but st_read(), st_point(), & st_as_sf() functions require the col type to be a numeric matrix .

How do I correct the column type or is there something easy I'm missing?

The values of your column seem to be in the well known text standard, and you can transform them to {sf} format using st_as_sf() and specifying the wkt and crs arguments.

So consider this example:


raw <- data.frame(label = c("point A",
                            "point B"),
                  geometry = c("POINT (-12.345 67.8910)",
                               "POINT (-12.543 67.0198)"))

clean <- raw %>% 
  st_as_sf(wkt = "geometry", # name of the column containg the WKT data
           crs = 4326) # coordinate reference system to make sense of the values


Note that I am making an assumption that your coordinates are lat/long in degrees; in the age of GPS this is arguably the most frequent case, but it still is an assumption on my side.

1 Like

This worked! Thank you!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.