I have a dataframe containing information about the activities of some organisations in different countries. The column orga contains the name of the organisations, c1 to c4 are country-columns containing the number of activities an organistion is doing in the country, and home is the organisation's country of residence. Values in home correspond to numbers in the column names of c1 to c4.
orga <- c("AA", "AB", "AC", "BA", "BB", "BC", "BD")
c1 <- c(3,1,0,0,2,0,1)
c2 <- c(0,2,2,0,1,0,1)
c3 <- c(1,0,0,1,0,2,0)
c4 <- c(0,1,1,0,0,0,0)
home <- c(1,2,3,2,1,3,1)
df <- data.frame(orga, c1, c2, c3, c4, home)
I know want to add an additional column foreign, containing information about all of an organisations foreign activities, summing up all activities mentioned in c1 to c4 but not in the column of the own country. So, the function should not sum up all the country-columns, but only the ones that are not the home-country. For example, if home=1 it should leave out c1, if home=2 leave out c2, etc.
In the example-case foreign should look like this:
df$foreign <- c(1,2,3,1,1,0,1)
Is there a way to sum up columns for different groups, leaving out a different column for every group, and add the sums as new column to a dataframe?
I already looked at the group by function of the dplyr-package, as well as aggregate and tapply in base-r, but couldn't come up with a solution. I would thus very much appreciate your help. Thank you!