[R] rowSums problem

John Kane jrkrideau at inbox.com
Tue Jun 5 22:21:35 CEST 2012


I am having a problem visualizing what you are doing here.  

What I see is a temp file of 760 elements.  From my point of view and reading in your data it would have a have dim(760, 1)  but your code seems to suggest that it is not a single vector.

Again I must be missing something completely as I would have thought that the output would have been a vector.  

Could exporting  the csv files have somehow mixed up the data structure that you are working with?

You might want to provide the two data files using the dput() command.  



John Kane
Kingston ON Canada


> -----Original Message-----
> From: vashchyshyn.i at gmail.com
> Sent: Tue, 5 Jun 2012 07:48:51 -0700 (PDT)
> To: r-help at r-project.org
> Subject: [R] rowSums problem
> 
> 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.

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list