Still a beginner in R and would appreciate any help with two questions about manipulating a dataframe df that is structured like this:
structure(list(country = c("Brazil", "Brazil", "Brazil", "France",
"France", "France"), date = structure(c(18353, 18354, 18355,
18353, 18354, 18355), class = "Date"), group = c(1, 1, 1, 1,
1, 1), share = c(0.00480432389150235, 0.00576122906219994, 0.00577170418006431,
0.0883595235712141, 0.0959849212276397, 0.104823151125402), max_share = c(0.110235379599597,
0.110235379599597, 0.110235379599597, 0.122052589288188, 0.122052589288188,
0.122052589288188)), row.names = c(NA, 6L), class = "data.frame")

(1) The variable max_share is the maximum share for each country observed between the initial date and end date. I came up with a rather clumsy way to add this variable into the df and was wondering if there is a more efficient way of doing this. Here's what I did:
df1 <- df %>%
group_by(country) %>%
summarize(max_share=max(share)) %>%
select(country, max_share)
df <- merge(df, df1, all=TRUE, by.x="country",by.y="country")
(2) I would like to create within the existing df two subgroups as follows.
I would like to add a second group variable called selection. I would like to pick those countries where max_share>0.10 and attribute for them the value 1 to selection.
Next, I would like to add to the df another country called Rest of the world. The Rest of the world observation will be labelled 2 under selection. Note that we will have several new observations as we have multiple dates for Rest of the world.
And then the bit I was struggling with. I would like that the share for these new observations is calculated as the sum of the share of the other countries under selection == 1.
I tried summarise and aggregate but could not identify how to keep the row at the same dimension, change the values for the non-numeric variables and only sum the numeric ones. There's probably a simple way to do this. Just couldn't figure it out.
In the end the df would contain the original set of countries, a new set of rows for the new observation Rest of the world (1 new row for each date), and a new column for the variable called selection.
Thanks for any help!