Dear list, This must be an easy one. I have a data frame like this one: test.df <- data.frame(x1=c(2,3,5), x2=c(5, 3, 4), w=c(0.8, 0.3, 0.5)) and I want to construct a weighted mean of the first two columns using the third column for weighting; i.e. y[1] = x1[1]*w[1] + x2[1]*(1-w[1]) y[2] = ... One way to do this is to use a loop like test.df$y <-numeric(3) with(test.df, for(i in 1:length(w)) { test.df$y[[i]] <<- weighted.mean(c(x1[[i]],x2[[i]]),c(w[[i]],1-w[[i]]) ) } ) My question is whether you can suggest a way to do the same without using a `for' loop, a vectorized version of this snippet - My actual dataset is large and it involves calculating the weighted mean of many columns. Such a loop becomes ugly to write and quite slow.... Thanks in advance, Vassilis -- View this message in context: http://r.789695.n4.nabble.com/weighed-mean-of-a-data-frame-row-by-row-tp3177421p3177421.html Sent from the R help mailing list archive at Nabble.com.
Hi Vassilis, Try test.df$y <- with(test.df, x1*w + x2*(1-w)) test.df HTH, Jorge On Thu, Jan 6, 2011 at 8:33 AM, Vassilis <> wrote:> > Dear list, > > This must be an easy one. I have a data frame like this one: > > test.df <- data.frame(x1=c(2,3,5), x2=c(5, 3, 4), w=c(0.8, 0.3, 0.5)) > > and I want to construct a weighted mean of the first two columns using the > third column for weighting; i.e. > > y[1] = x1[1]*w[1] + x2[1]*(1-w[1]) > y[2] = ... > > One way to do this is to use a loop like > > test.df$y <-numeric(3) > > with(test.df, > for(i in 1:length(w)) { > test.df$y[[i]] <<- weighted.mean(c(x1[[i]],x2[[i]]),c(w[[i]],1-w[[i]]) ) } > ) > > My question is whether you can suggest a way to do the same without using a > `for' loop, a vectorized version of this snippet - My actual dataset is > large and it involves calculating the weighted mean of many columns. Such a > loop becomes ugly to write and quite slow.... > > Thanks in advance, > > Vassilis > > > -- > View this message in context: > http://r.789695.n4.nabble.com/weighed-mean-of-a-data-frame-row-by-row-tp3177421p3177421.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Em 6/1/2011 11:33, Vassilis escreveu:> > Dear list, > > This must be an easy one. I have a data frame like this one: > > test.df <- data.frame(x1=c(2,3,5), x2=c(5, 3, 4), w=c(0.8, 0.3, 0.5)) > > and I want to construct a weighted mean of the first two columns using the > third column for weighting; i.e. > > y[1] = x1[1]*w[1] + x2[1]*(1-w[1]) > y[2] = ... > > One way to do this is to use a loop like > > test.df$y <-numeric(3) > > with(test.df, > for(i in 1:length(w)) { > test.df$y[[i]]<<- weighted.mean(c(x1[[i]],x2[[i]]),c(w[[i]],1-w[[i]]) ) } > ) > > My question is whether you can suggest a way to do the same without using a > `for' loop, a vectorized version of this snippet - My actual dataset is > large and it involves calculating the weighted mean of many columns. Such a > loop becomes ugly to write and quite slow.... > > Thanks in advance, >Vassilis, Is this what you're looking for? > test.df$y <- test.df$x1 * test.df$w + test.df$x2 * (1 - test.df$ w) > test.df$y [1] 2.6 3.0 4.5 -- Cesar Rabak
Thanks for the help guys! For my purpose I think that rharlow2's answer, i.e. the `rowSums' function is the most appropriate since it also takes care of the NAs. Best, Vassilis -- View this message in context: http://r.789695.n4.nabble.com/weighed-mean-of-a-data-frame-row-by-row-tp3177421p3178912.html Sent from the R help mailing list archive at Nabble.com.