3
votes

In my dataframe, see table attached here, I have three columns: country, results and eurosceptic.

I would like to know if it is possible to merge together rows that share all but two observations, which should be the results and eurosceptic observations.

For example, a function that would leave me with two rows for Belgium. One in which the eurosceptic value is 1 and one where it's 0. Then, the results column in each of those rows would be the sum formed by the results of the former rows that shared either 1 or 0 for the eurosceptic variable.

So, the eurosceptic = 0 Belgium row would have its results observation equal the sum of the results observations of the rows in my current table, which were related to Belgium and all had the eurosceptic value as 0.

In short, a transformation of my df to one with two rows per country, the eurosceptic value as 0 and 1, where the results observation for each is the summed results observations of the previous rows with the corresponding country and eurosceptic values.

Is this possible?

Thank for your help in advance!

My table as it is now

1
Welcome to SO. Please check this on how to make a minimal, reproducible example. And this one on R examples.jake2389

1 Answers

1
votes

We can group by 'Country', 'eurosceptic' get the sum of 'results'

library(dplyr)
 df1 %>%       
     group_by(Country, eurosceptic) %>%         
     summarise(results = sum(results))