[R] rowSums problem

Rui Barradas ruipbarradas at sapo.pt
Tue Jun 5 22:18:07 CEST 2012


Hello,

The files you've uploaded are the weights file and the results file, not 
the original temp.csv.
So this is untested but it seems you have a standard matrix multiply 
problem.

temp3880W <- temp[, 3:50] %*% weight3880

Hope this helps,

Rui Barradas

Em 05-06-2012 15:48, alonis10 escreveu:
> I'm having a very frustrating problem, trying to find the inverse distance
> squared weighted interpolants of some weather data.
>
> I have a data frame of weights, which sum to 1.  I have attached the weights
> data. I also have a data frame of temperatures at 48 grid points, which I
> have also attached.
>
> Now, all I need to do is multiply all of the rows of the temperature data
> frame by the weights (element-wise), and sum across the columns.
>
> However, when I try to use the most obvious approach,
>
> temp3880W<-  weight3880*temp[,(3:50)]
> temp3880W<- rowsum(temp3880W)
>
>
> I get the wrong result:
>
>
> head(temp3880W)
>           1          2          3          4          5          6
> -0.4904454 -1.2728543 -1.5360133 -0.2687030 62.3048012  6.2610305
>
>
>
> I've only been successful by using a for loop which is far too slow:
>
> temp3880<- rep(0,length(temp$Year))
>
> for (i in 1:length(temp$Year)) {
> wmul<- weight3880*as.vector(temp[i,(3:50)])
> temp3880[i]<- sum(wmul)
> }
>
>
> This gives the result
>
> head(temp3880)
> [1] -6.936374 -9.617799 -7.227260  1.135293  8.973817 13.632454
>
>
>
> Can anyone point out to me what is going wrong here? I've tried the first
> approach with smaller data frames and vectors and it seems to work fine, so
> I must be making a mistake somewhere...
>
> Thank you!
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/rowSums-problem-tp4632405.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list