From Me, 3 Months ago, written in Text.
Embed
  1. rm(list=ls())
  2.  
  3. source('utils.r')
  4.  
  5. # The data; change path here if necessary
  6. map0 <- raster('data/lc_saloum_1999.tif')
  7. map1 <- raster('data/lc_saloum_2010.tif')
  8.  
  9. # Matrix of change in pixels count
  10. matrix.change <- table(as.vector(map0), as.vector(map1))
  11.  
  12. # Marginal sums
  13. row.sum <- apply(matrix.change, 2, sum)
  14. col.sum <- apply(matrix.change, 1, sum)
  15.  
  16. # Matrix of change: 1999 -> 2010
  17. matrix.change.99.10 <- apply(matrix.change, 1, function(row) row/row.sum) * 100
  18.  
  19. # Matrix of change: 2010 -> 1999
  20. matrix.change.10.99 <- apply(matrix.change, 2, function(col) col/col.sum) * 100
  21.  
  22. # Percentage of change
  23. percentage.change <- (1 - sum(diag(matrix.change)) / sum(matrix.change)) * 100
  24.  
  25. # Save results
  26. write.csv(matrix.change.10.99, file="change_10_99.csv", sep=",")
  27. write.csv(matrix.change.99.10, file="change_99_10.csv", sep=",")
  28. write.csv(matrix.change, file="matrix_change.csv", sep=",")
  29.  
  30.  
  31.  
  32.