# [R] Extracting values from grid cells using for loops

David Winsemius dwinsemius at comcast.net
Sun May 19 18:06:13 CEST 2013

```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
>  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

```