Quantcast

weighed mean of a data frame row-by-row

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

weighed mean of a data frame row-by-row

Vassilis
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

Bob
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: weighed mean of a data frame row-by-row

Bob
This post has NOT been accepted by the mailing list yet.
Try something like this:

test.df <- data.frame(x1=c(2,3,5), x2=c(5, 3, 4), w=c(0.8, 0.3, 0.5))
wgt.df <- cbind(test.df[,3], 1-test.df[,3])
ans <- rowSums(test.df[,1:2]*wgt.df)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: weighed mean of a data frame row-by-row

Jorge I Velez
In reply to this post by Vassilis
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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: weighed mean of a data frame row-by-row

csrabak
In reply to this post by Vassilis
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: weighed mean of a data frame row-by-row

Vassilis
In reply to this post by Bob
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
Loading...