I want to calculate the weighted average of different columns with different names. For example, weight4 is multiplied by 3, weight8 is multiplied by 2 and weight12 is multiply by 1, then we add all the numbers and divided by 6. This is an example, my real data has 53 variables, each one of them is measure in different timeframes denoted by the number at the end of the variables.
This is my dataset
weight4 <- c(4,2,3,4,5,6)
height4 <- c(4,2,3,3,5,8)
size4 <- c(2,3,5,6,2,3)
weight8 <- c(4,2,3,4,5,6)
height8 <- c(4,2,3,3,5,8)
size8 <- c(2,3,5,6,2,3)
weight12 <- c(4,2,3,4,5,6)
height12 <- c(4,2,3,3,5,8)
size12 <- c(2,3,5,6,2,3)
df <- data.frame(weight4, height4, size4, weight8, height8, size8,
weight12, height12, size12)
I want to calculate the weighted average of these columns: This is what I have so far
Z <- unique(gsub("\..", "", grep("^\[0-9]+$", names(df), value = TRUE)))
Z
new <- lapply(setNames(nm = Z), function(z) {
Zs <- grep(paste0("^", z, "\"), names(df))
Zs_seq <- rev(seq_along(Zs))
as.matrix(df[Zs]) %*% matrix(Zs_seq, ncol = 1) / sum(Zs_seq)
})
But it doesn't work for my data. Any Idea on how I can change this function for my data?
This line of code works for each variable:
weight <- grep("weight", names(df))
Xs_seq <- rev(seq_along(weight))
weight <- as.matrix(df[weight]) %*% matrix(Xs_seq, ncol = 1) / sum(Xs_seq)
df$weight <- weight
I would like to know if there is a way in which I can make R looks for the pattern in the data without having to manually use each variable in the data frame
question from:
https://stackoverflow.com/questions/65910925/how-can-i-use-gsub-and-grep-to-calculate-the-weighted-average-in-r