I am doing a change matrix of two periods of Land Cover in R that I usually/easily do in Excel using pivot table.
I got how to do the pivot table way in R but I'm having a bit trouble in manipulating the data frame further.
I NEED TO HAVE THE EXACT ROWS AND COLUMNS from data frames with unequal rows and columns and just put 0 to the missing rows/columns.
Sample:
df <- matrix(1:20, 4)
colnames(df) <- c('a', 'b', 'c', 'd', 'e')
rownames(df) <- c('a', 'b', 'c', 'd')
df1 <- df[-3,]
df1
In 'df1', rows 'c' and 'e' is missing. I started making a for loop that goes into rows and look for what's missing:
FinalTable <- function (df){
for (i in rownames(df)){
if (i != colnames(df)){
print ('not equal')
}
}
}
After this, I need to do rbind to add the column that is missing in the row.
I appreciate your help.
I can send the raw and desired table for your reference.
Thanks!
Nando