Question: I have this data in a spreadsheet

```
Country       Sales
Spain 1       1000
Spain 2       200
France        300
Nigeria 1     500
Nigeria 2     700

```

I want the sum of this country's sales stored a seperate dataframe.

I tried using the dplyr function but the result is not what i want

Here is the output I want please

```
Country   Sum_of_sales

Spain     1200
France    300
Nigeria   1200

```

Is there a way I can run this on R that will give me this above output stored in a separate dataframe please.

Answer: 
If you wanna keep it `tidyverse`, you can use `extract`:

```r
library(tidyr)
library(dplyr)
df %>% 
  extract(Country, "Country") %>% 
  group_by(Country) %>% 
  summarise(Sum_of_sales = sum(Sales))

# A tibble: 3 × 2
  Country Sum_of_sales
  <chr>          <int>
1 France           300
2 Nigeria         1200
3 Spain           1200

```

For more complex cases, you can use:

```r
extract(Country, "Country", "([A-Za-z -]*)\\s*[0-9]*")

```
An option with `str_remove`

```
library(dplyr)
library(stringr)
df %>% 
  group_by(Country = str_remove(Country, "\\s+\\d+")) %>% 
  summarise(Sum_of_sales = sum(Sales))

```

-output

```
# A tibble: 3 × 2
  Country Sum_of_sales
  <chr>          <int>
1 France           300
2 Nigeria         1200
3 Spain           1200

```