I'm trying to apply spatial data mining to a set of vector and raster files so I need a way to convert my raster archives into a CSV in order to run the mining

A little bit of background, my thesis is about applying data mining in archeology with the intention of modeling archaeological sites

  • 1
    Welcome to GIS SE. Thank you for taking the Tour. We are a problem-solving site, so Questions always need to be asked in a way that answers the two most commonly commented questions, "What have you tried?" and "Where are you stuck?" I suspect you may have an XY Problem here, since CSV is not necessary to access raster data (it is, in fact, the least useful form of raster data), so you probably want to ask about the root problem, not this solution.
    – Vince
    Sep 13, 2022 at 13:56

1 Answer 1


You can use the raster and sf R packages to do this.

# Install packages (uncomment)

# Load packages

# Read raster data in GeoTIFF format
r <- raster::stack("C:/folder/my_raster.tif")
# Convert RasterStack object into data.frame with XY coordinates
r_df <- as.data.frame(r, xy=TRUE)
# Export data.frame to CSV
write.csv(r_df, "F:/folder/my_raster.csv")

# Read vector data in ESRI Shapefile format
# The geometry column contains the shape of your entities
s <- sf::st_read("C:/folder/my_shape.shp")
# Convert sf object into data.frame
s_df <- data.frame(s)
# Export data.frame to CSV
write.csv(s, "F:/folder/my_shape.csv")

You can use head(r_df) and head(s_df) to see the head of your data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.