If you want to do this in base R programming, then refer to the following code:
df$norm <- with(df, X/ave(X, Pair, FUN = sum))
df
# Pair X norm
#1 1 2 0.40
#2 1 3 0.60
#3 2 1 0.33
#4 2 2 0.67
The 2nd way is with dplyr package:
library(dplyr)
df %>% group_by(Pair) %>% mutate(norm = X/sum(X))
and data.table:
library(data.table)
setDT(df)[, norm := X/sum(X), Pair]
data:
df <- structure(list(Pair = c(1L, 1L, 2L, 2L), X = c(2L, 3L, 1L, 2L
)), class = "data.frame", row.names = c(NA, -4L))