[R] Extracting values from grid cells using for loops

Jess Baker jessbaker1990 at hotmail.co.uk
Sun May 19 13:20:14 CEST 2013


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

Many thanks,
Jess 		 	   		  


More information about the R-help mailing list