Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

regex - How can I use gsub and grep to calculate the weighted average in R?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...