I would like to create a cross tabulation table with table and addmargins and then add a column from another data frame to the table.
Example data:
df <- data.frame(country = c("B", "A", "C", "C", "A", "A", "B", "C", "C"), year = c(2005, 2006, 2005, 2007, 2005, 2005, 2007, 2006, 2006))
df2 <- data.frame(country = c("C", "B", "A", "C", "A", "B", "C", "A", "A", "A", "B", "C", "B", "A", "C"))
I would like to combine:
addmargins(table(df$country, df$year), FUN = list(Total = sum))
addmargins(table(df2$country), FUN = list(Total = sum))
such that I get
2005 2006 2007 Total df2
A 2 1 0 3 6
B 1 0 1 2 4
C 1 2 1 4 5
Total 4 3 2 9 15