[R] rowSums problem

alonis10 vashchyshyn.i at gmail.com
Tue Jun 5 16:48:51 CEST 2012


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.



More information about the R-help mailing list