Session Ready
Exercise

Converting to and from coordinates

In order to convert a data frame of coordinates into an sf object you can make use of the st_as_sf() function you used in the previous exercise. You can specify the coords argument with the names of the coordinate variables (with the X coordinate/longitude coordinate listed first) and, optionally, the crs argument if you know the CRS of your coordinates. The CRS can be specified as a proj4 string or EPSG code.

If you want to convert your sf point objects to a data frame with coordinates, you can use the st_write() function with a hidden argument (these are arguments associated with an external utility called GDAL and so they're not in the R help) to force sf to include the coordinates in the output file. The argument you need is layer_options = "GEOMETRY=AS_XY".

Instructions
100 XP
  • Read in the "trees.csv" file with read.csv() and call this trees.
  • Convert the trees object you just created to an sf object with st_as_sf(). Set the crs to 4326.
  • Plot the geometry of the points with plot() and st_geometry() to confirm conversion to sf worked.
  • Write this points object out to CSV with the coordinates using st_write(), call the file "new_trees.csv".
  • Read in the CSV you just created with read.csv() and confirm that the coordinates exist with head().