R - Weighted Mean by row for multiple columns based on columns string values

Solution 1:

I guess you could get your weight vector like this:

library(tidyverse)

weights_precursor <- str_split(names(data)[-1], pattern = "\\.", n = 2, simplify = TRUE)[, 1] %>% 
  as.numeric()

weights <- 2305.2 * weights_precursor ^ -1.019