I have a shapefile (sf) object made up of polygons with polygon characteristics stored in a dataframe. This object is called
tiles. Within the dataframe there are 12 variables indicating number of pixels that transitioned to zero forest cover by year called
loss2001, loss 2002, ... loss2012. Each variable is indexed by a unique identifier
library(sf) tiles = st_read("ShapefileForestlossGroup1")
I want to reshape this shapefile dataframe so that I can produce annual spatial weights. The problem is that many reshaping options are best executed with a data table, but if I transform
tiles into a data table, it is no longer an sf object.
As an sf object with a dataframe, I tried executing the following reshape function:
tiles2 = reshape(tiles, direction = "long", varying = list(names(tiles)[1:12]), v.names = "numPixelsDeforested", idvar = "ID", timevar = "Year", times = 2001:2012)
But R returns the following error:
Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length In addition: Warning message: In `[<-.data.frame`(`*tmp*`, , v.names, value = list(loss2001 = c(0L, : provided 2 variables to replace 1 variables
Could these errors be related to the fact that this is an sf object dataframe, or is the syntax just wrong?