Вы можете группировать по странам, а затем делить продажи на максимальные продажи - я полагаю, что это total
.
library(dplyr)
df %>%
group_by(year) %>%
mutate(ratio = sales / max(sales))
# A tibble: 6 x 4
# Groups: year [2]
# country year sales ratio
# <chr> <int> <int> <dbl>
#1 Afghanistan 1950 30 1
#2 Afghanistan 1951 35 0.875
#3 Albania 1950 0 0
#4 Albania 1951 5 0.125
#5 total 1950 30 1
#6 total 1951 40 1
В base R
transform(df, ratio = ave(sales, year, FUN = function(x) x / max(x)))
Или с data.table
library(data.table)
setDT(df)[, ratio := sales / max(sales), by = year][]
данные
df <- structure(list(country = c("Afghanistan", "Afghanistan", "Albania",
"Albania", "total", "total"), year = c(1950L, 1951L, 1950L, 1951L,
1950L, 1951L), sales = c(30L, 35L, 0L, 5L, 30L, 40L)), .Names = c("country",
"year", "sales"), class = "data.frame", row.names = c(NA, -6L
))