[R] Extracting values from grid cells using for loops

2 views
Skip to first unread message

Jess Baker

unread,
May 19, 2013, 7:20:14 AM5/19/13
to r-h...@r-project.org
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
______________________________________________
R-h...@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.

David Winsemius

unread,
May 19, 2013, 12:06:13 PM5/19/13
to Jess Baker, r-h...@r-project.org

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

Bert Gunter

unread,
May 19, 2013, 1:08:05 PM5/19/13
to David Winsemius, r-h...@r-project.org
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

--

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

Reply all
Reply to author
Forward
0 new messages