Sum non-conformable matrices by rowname in r

100 views Asked by At

I have two unequally sized matrices, here is a small sample (my real matrices are much larger):

s <- matrix(1:36, nrow = 6, ncol = 6, byrow=TRUE, dimnames = list(c("Afghanistan","Albania","Algeria","Argentina","Australia","Austria"), c("Afghanistan","Albania","Algeria","Argentina","Australia","Austria")))
s2 <- matrix(1:18, nrow = 3, ncol = 3, byrow=TRUE, dimnames = list(c("Albania","Algeria","Australia"), c("Albania","Algeria","Australia")))

I would like to sum country values by rowname. Since only some rownames exist in both matrices, I need to match the rownames to each other and then sum the values where there is a match. How can I do this?

This is the output that I want, with values from "s2" added to the first matrix "s":

enter image description here

Here are dput's of my full data:

> dput(head(s))
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9937, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 281786, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 1.33333333333333, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 33000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 498766.333333333, 0, 0, 0, 
0, 0, 103470.333333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0), .Dim = c(6L, 179L), .Dimnames = list(c("Afghanistan", 
"Albania", "Algeria", "Antigua and Barbuda", "Argentina", "Armenia"
), c("Afghanistan", "Albania", "Algeria", "Antigua and Barbuda", 
"Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", 
"Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", 
"Belize", "Bermuda", "Bhutan", "Bolivia (Plurinational State of)", 
"Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", 
"Bulgaria", "Burkina Faso", "Burundi", "Côte d'Ivoire", "Cabo Verde", 
"Cambodia", "Cameroon", "Canada", "Central African Republic", 
"Chile", "China, Hong Kong SAR", "China, Macao SAR", "China, mainland", 
"China, Taiwan Province of", "Colombia", "Comoros", "Congo", 
"Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia", 
"Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", 
"Ecuador", "Egypt", "El Salvador", "Estonia", "Eswatini", "Ethiopia", 
"Faroe Islands", "Fiji", "Finland", "France", "French Polynesia", 
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Greenland", 
"Guatemala", "Guinea", "Guyana", "Honduras", "Hungary", "Iceland", 
"India", "Indonesia", "Iran (Islamic Republic of)", "Ireland", 
"Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", 
"Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Latvia", "Lebanon", 
"Libya", "Lithuania", "Luxembourg", "Madagascar", "Malawi", "Malaysia", 
"Maldives", "Mali", "Malta", "Mauritania", "Mauritius", "Mexico", 
"Mongolia", "Montenegro", "Morocco", "Namibia", "Nepal", "Netherlands", 
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", 
"North Macedonia", "Norway", "Oman", "Pakistan", "Panama", "Papua New Guinea", 
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", 
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation", 
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", 
"Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", 
"Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", 
"Solomon Islands", "South Africa", "Spain", "Sri Lanka", "Suriname", 
"Sweden", "Switzerland", "Thailand", "Togo", "Tonga", "Tunisia", 
"Turkey", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom of Great Britain and Northern Ireland", 
"United Republic of Tanzania", "United States of America", "Uruguay", 
"Vanuatu", "Venezuela (Bolivarian Republic of)", "Yemen", "Zambia", 
"Zimbabwe", "Unspecified Area", "Viet Nam", "Syrian Arab Republic", 
"Myanmar", "Benin", "Mozambique", "Trinidad and Tobago", "Lao People's Democratic Republic", 
"Tajikistan", "Ang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, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 3212.64, 22.62, 0.09, 0.09, 67.92, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 9.72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(6L, 181L), .Dimnames = list(
    c("Albania", "Algeria", "Antigua and Barbuda", "Argentina", 
    "Armenia", "Aruba"), c("Albania", "Algeria", "Antigua and Barbuda", 
    "Argentina", "Armenia", "Aruba", "Australia", "Austria", 
    "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", 
    "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", 
    "Bolivia (Plurinational State of)", "Bosnia and Herzegovina", 
    "Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", 
    "Burundi", "Côte d'Ivoire", "Cabo Verde", "Cambodia", "Cameroon", 
    "Canada", "Central African Republic", "Chile", "China, Hong Kong SAR", 
    "China, Macao SAR", "China, mainland", "China, Taiwan Province of", 
    "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", 
    "Croatia", "Cuba", "Cyprus", "Czechia", "Democratic Republic of the Congo", 
    "Denmark", "Djibouti", "Dominica", "Ecuador", "Egypt", "El Salvador", 
    "Estonia", "Eswatini", "Ethiopia", "Faroe Islands", "Fiji", 
    "Finland", "France", "French Polynesia", "Gabon", "Gambia", 
    "Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada", 
    "Guatemala", "Guinea", "Guyana", "Honduras", "Hungary", "Iceland", 
    "India", "Indonesia", "Iran (Islamic Republic of)", "Ireland", 
    "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", 
    "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Latvia", "Lebanon", 
    "Libya", "Lithuania", "Luxembourg", "Madagascar", "Malawi", 
    "Malaysia", "Maldives", "Mali", "Malta", "Mauritania", "Mauritius", 
    "Mexico", "Mongolia", "Montenegro", "Montserrat", "Morocco", 
    "Namibia", "Nepal", "Netherlands", "New Caledonia", "New Zealand", 
    "Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway", 
    "Oman", "Pakistan", "Panama", "Papua New Guinea", "Paraguay", 
    "Peru", "Philippines", "Poland", "Portugal", "Qatar", "Republic of Korea", 
    "Republic of Moldova", "Romania", "Russian Federation", "Rwanda", 
    "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", 
    "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", 
    "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", 
    "Solomon Islands", "South Africa", "Spain", "Sri Lanka", 
    "Suriname", "Sweden", "Switzerland", "Thailand", "Togo", 
    "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Tuvalu", 
    "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom of Great Britain and Northern Ireland", 
    "United Republic of Tanzania", "United States of America", 
    "Uruguay", "Vanuatu", "Venezuela (Bolivarian Republic of)", 
    "Yemen", "Zambia", "Zimbabwe", "South Sudan", "Unspecified Area", 
    "Viet Nam", "Myanmar", "Equatorial Guinea", "Dominican Republic", 
    "Syrian Arab Republic", "Mozambique", "Angola", "Democratic People's Republic of Korea"
    )))


Afghanistan for example is included in "s" but not in "s2".

5

There are 5 answers

1
Arual On

I think this is what you are looking for. I do not know if it is the most optimal way but it works.

s <- matrix(1:36, nrow = 6, ncol = 6, byrow=TRUE, dimnames = list(c("Afghanistan","Albania","Algeria","Argentina","Australia","Austria"), c("Afghanistan","Albania","Algeria","Argentina","Australia","Austria")))
s2 <- matrix(1:18, nrow = 3, ncol = 3, byrow=TRUE, dimnames = list(c("Albania","Algeria","Australia"), c("Albania","Algeria","Australia")))

s_new<-s[rownames(s) %in% rownames(s2),]
s_new<-s_new[, colnames(s_new) %in% colnames(s2)]

s_new<-s_new[,order(colnames(s_new))]
s2<-s2[,order(colnames(s2))]

Final<-s_new+s2

s1<-s
for( i in 1:dim(s1)[2])
{
  for( j in 1:dim(s1)[1])
  {
  s1[i,j]<-ifelse(rownames(s1)[i] %in% rownames(Final) & colnames(s)[j] %in% colnames(Final), 
                  Final[which(rownames(Final)==rownames(s)[i]),which(colnames(Final)==colnames(s)[j])],s1[i,j])
  }
}
0
Arual On

This should do.

s1<-s
for( i in 1:dim(s1)[2])
{
  for( j in 1:dim(s1)[1])
  {
    s1[i,j]<-ifelse(rownames(s1)[i] %in% rownames(s2) & colnames(s)[j] %in% colnames(s2), 
                    s1[i,j]+s2[which(rownames(s2)==rownames(s)[i]),which(colnames(s2)==colnames(s)[j])],s1[i,j])
  }
}
10
GKi On

You can rbind the to a data.frame converted matrix and use xtabs to sum it up:

tt <- rbind(as.data.frame(as.table(s)), as.data.frame(as.table(s2)))
xtabs(Freq ~ Var1 + Var2, tt)
#             Var2
#Var1          Afghanistan Albania Algeria Argentina Australia Austria
#  Afghanistan           1       2       3         4         5       6
#  Albania               7       9      11        10        14      12
#  Algeria              13      18      20        16        23      18
#  Argentina            19      20      21        22        23      24
#  Australia            25      33      35        28        38      30
#  Austria              31      32      33        34        35      36
0
ThomasIsCoding On

You can try the code below

s2names <- dimnames(s2)
s[s2names[[1]],s2names[[2]]] <- s[s2names[[1]],s2names[[2]]] + s2

such that

            Afghanistan Albania Algeria Argentina Australia Austria
Afghanistan           1       2       3         4         5       6
Albania               7       9      11        10        14      12
Algeria              13      18      20        16        23      18
Argentina            19      20      21        22        23      24
Australia            25      33      35        28        38      30
Austria              31      32      33        34        35      36
1
Arual On

I hope I understood correctly. If not, please provide the matrix that you want to reach as a final output.

s <- matrix(1:36, nrow = 6, ncol = 6, byrow=TRUE, dimnames = list(c("Afghanistan","Albania","Algeria","Argentina","Australia","Austria"), c("Afghanistan","Albania","Algeria","Argentina","Australia","Austria")))
s2 <- matrix(1:18, nrow = 3, ncol = 3, byrow=TRUE, dimnames = list(c("Albania","Algeria","Australia"), c("Albania","Algeria","Australia")))

s_new<-s[rownames(s) %in% rownames(s2),]
s_new<-s_new[, colnames(s_new) %in% colnames(s2)]

s_new<-s_new[,order(colnames(s_new))]
s2<-s2[,order(colnames(s2))]

Final<-s_new+s2