[R] Extracting values from grid cells using for loops

Bert Gunter gunter.berton at gene.com
Sun May 19 19:08:05 CEST 2013


Also ?tapply

But if you are a beginner who has done no homework -- i.e. you have
made no effort to learn basics with The Intro to R tutorial or other
online tutorial -- then you probably won't be able figure it out. We
expect some minimal effort by posters. If you have done such homework,
then it may be an alternative for you.

-- Bert

On Sun, May 19, 2013 at 9:06 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On May 19, 2013, at 4:20 AM, Jess Baker wrote:
>
>> Dear list,
>>
>> I am very new to R and have been struggling with extracting data from a netcdf file. Basically I have read in a file containing vegetation height data organised in 0.5 degree grid cells spanning the whole globe. Each cell contains a histogram representing the height distribution and I need to extract a single value (the mean) from each cell. My data has 720 rows and 240 columns so is pretty large and I’ve been told for loops should be the most straightforward way to do this. Sorry the code below is not reproducible but hopefully it illustrates where I am going wrong.
>>
>> ##The following returns the mean value of a single cell (260,90)
>>
>>> sum(ht.center.vals*(ht.hist[260,90,]))/sum(ht.hist[260,90,])
>>
>> ##but when I try to translate this to a for loop I get NaN
>>
>>> mean.height<-NULL
>>> for(i in 1:nrow(ht.hist)){
>> + for(j in 1:ncol(ht.hist)){
>> + mean.height<-sum(ht.center.vals*(ht.hist[i,j,]))/sum(ht.hist[i,j,])
>> + }
>> + }
>>> mean.height
>> [1] NaN
>
> You appear to be committing a common programming mistake. Assignment on the LHS to a non-indexed variable will overwrite all of the previous values. The NaN is now simply the last value calculated.
>
>
>> Can anyone tell me how I tell R to look at each cell in turn, extract the mean value and save it in a new array? Clearly I am not doing it correctly!
>
> I would imagine making mean.height be an matrix/array of the desired dimensions and then assign to the proper values inside that object using the loop indices.
>
> mean.height <- matrix(NA, nrow(ht.hist), ncol(ht.hist) )
> # then the loop would do the assignment:
>
> mean.height[,i,j] <- …
>
>
> --
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list