I have a large dataframe that looks simplified like this:
df <- data.frame(Code = c("AUS1", "AUS2", "AUS3", "AUT1", "AUT2", "AUT3", "BEL1", "BEL2", "BEL3"),
AUS1 = c(3, 45, 1, 65, 817, 235, 1223, 234, 867),
AUS2 = c(354, 12, 843, 346, 9754, 123, 4638, 988, 4),
AUS3 = c(67, 82, 9485, 127, 347, 505, 123, 2, 3),
AUT1 = c(1182, 943, 3, 12, 345, 174, 12, 4, 12),
AUT2 = c(78, 8882, 17, 49, 2, 958, 76, 24, 198),
AUT3 = c(1, 99, 300, 17, 389, 234, 122, 62, 91),
BEL1 = c(88, 192, 943, 199, 238, 1294, 1, 4,35),
BEL2 = c(983, 112, 538, 1274, 22, 94, 100, 84, 7),
BEL3 = c(41, 8819, 237, 11, 347, 12, 871, 34, 1))
I know want to summarise each row and each column on two different conditions.
First, I need a sum that excludes the values in which the Code matches the same three laters as the column names. Example: The sum of the first column (AUS1) should exclude the values of the rows with values from the first column (Code) also start with "AUS". For the fourth column (AUT1), the sum should exclude row values which have values of the first column (Code) that start with "AUT" and so on.
The desired output for the colum sums then would be: AUS1 = 3441, AUS2 = 15853, AUS3 = 1107, AUT1 = 2156, AUT2 = 9275, AUT = 675, BEL1 = 2954, BEL2 = 3023, BEL3 = 9467
After that, I would have to sum the rows on the same condition.
Second, I have to again sum each column and each row, but this time it should exclude only the value of the direct match. Example: For the sum of the second column (AUS1), it should only exclude the first row where AUS1 == AUS1. For the third column (AUS2), it should only exclude the value of the second row where AUS2 == AUS2.
Since my dataframes are quite large i cannot do this manually, but rather a function would be helpful.