My apologies. This is my first time using Stackoverflow, so I'm not used to posting questions. Here's what I'm coding
library(raster)
library(landscapemetrics)
library(landscapetools)
# Add raster data for 2000
hex1_2000<-raster('2000_hex1.tif')
hex2_2000<-raster('2000_hex2.tif')
hex3_2000<-raster('2000_hex3.tif')
hex4_2000<-raster('2000_hex4.tif')
...
hex23_2000<-('2000_hex4.tif')
# Add raster data for 2010
hex1_2010<-raster('2010_hex1.tif')
hex2_2010<-raster('2010_hex2.tif')
hex3_2010<-raster('2010_hex3.tif')
hex4_2010<-raster('2010_hex4.tif')
...
hex23_2010<-('2000_hex4.tif')
#Create data frame as table
hex1 = data.frame(
lc00 = values(hex1_2000),
lc10 = values(hex1_2010))
hex2 = data.frame(
lc00 = values(hex2_2000),
lc10 = values(hex2_2010))
hex3 = data.frame(
lc00 = values(hex3_2000),
lc10 = values(hex3_2010))
hex4 = data.frame(
lc00 = values(hex4_2000),
lc10 = values(hex4_2010))
...
hex23 = data.frame(
lc00 = values(hex23_2000),
lc10 = values(hex23_2010))
...
hex1 = table(hex1[,c('lc00','lc10')])
hex2 = table(hex2[,c('lc00','lc10')])
hex3 = table(hex3[,c('lc00','lc10')])
hex4 = table(hex4[,c('lc00','lc10')])
...
hex23 = table(hex23[,c('lc00','lc10')])
#Define crosstabulation matrix
Hex1_Trans = as.matrix(hex1 / rowSums(hex1))
write.csv(Hex1_Trans, 'hex1Trans.csv')
Hex2_Trans = as.matrix(hex2 / rowSums(hex2))
write.csv(Hex2_Trans, 'hex2Trans.csv')
Hex3_Trans = as.matrix(hex3 / rowSums(hex3))
write.csv(Hex3_Trans, 'hex3Trans.csv')
Hex4_Trans = as.matrix(hex2 / rowSums(hex4))
write.csv(Hex4_Trans, 'hex2Trans.csv')
...
Hex23_Trans = as.matrix(hex23 / rowSums(hex23))
write.csv(Hex23_Trans, 'hex23Trans.csv')
As you can see, there are innumerous instances where I'm repeating the same process. I would be delighted to know how I can make this code simpler and more elegant. My coding is always like this, and I find this obviously highly inefficient. Thank you everyone for your help.