I am using gapminder data to subtract values of 1 country gdpPercapita from rest of the countries.
I have referred to the link on same topic but still getting issues. Subtract a column in a dataframe from many columns in R)
Issue When I subtract a column (eg. India) from all other countries then in return I am getting India as 0 values (which is correct) but it didn't subtract India from other columns for example Vietnam.
Pivoting data for comparing India, Vietnam before Subtraction
gapminder %>%
select(country, year, gdpPercap) %>%
pivot_wider(names_from = country, values_from = gdpPercap) %>%
arrange(year) %>%
select(year,India,Vietnam)
year India Vietnam
<int> <dbl> <dbl>
1952 546.5657 605.0665
1957 590.0620 676.2854
1962 658.3472 772.0492
1967 700.7706 637.1233
1972 724.0325 699.5016
1977 813.3373 713.5371
1982 855.7235 707.2358
Comparing India, Vietnam after Subtraction
gapminder %>%
select(country, year, gdpPercap) %>%
pivot_wider(names_from = country, values_from = gdpPercap) %>%
arrange(year) %>%
mutate_at(vars(-matches("year")), ~ . - India) %>%
select(year,India,Vietnam)
year India Vietnam
<int> <dbl> <dbl>
1952 0 605.0665
1957 0 676.2854
1962 0 772.0492
1967 0 637.1233
1972 0 699.5016
1977 0 713.5371
1982 0 707.2358
I am not sure what is wrong with the code ?
Appreciate any help !!