[R] Creating a loop to read 3D dataset

Greg Snow Greg.Snow at imail.org
Thu Jul 23 23:49:31 CEST 2009


Something like this (untested) may work for you:

> for (i in 1:132) {
+  a <- clim[ , , i]
+  nm <- sprint('clim%03d.txt',i)
+  write.table(a,nm)
+ }

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Andrew Aldersley
> Sent: Thursday, July 23, 2009 2:38 PM
> To: r-help at r-project.org
> Subject: [R] Creating a loop to read 3D dataset
> 
> 
> Dear all,
> 
> I have in my possession a netcdf from which I want to extract some data
> files. I have used the "ncdf" package to read the netcdf file and used
> the "get.var.ncdf" function to identify the variable i wish to use. The
> data is in the form of a time-series of geographical data points that
> relate to climatology variables. As such I have a large data frame,
> "clim", of the following dimensions:
> 
> [720, 360, 132]
> 
> The 720 and 360 values relate to longitude and latitude points on the
> globe and there are 132 different time-series grids. What I wish to do
> is extract each of the individual time-series data grids and save them
> separately. So something along the lines of...
> 
> a <- clim[1:720, 1:360, 1]
> b <- clim[1:720, 1:360, 2]
> c <- clim[1:720, 1:360, 3]
> 
> ...and so on, all the way up to 132.
> 
> To do this manually for each different z-coordinate of "clim" and save
> them is possible but requires me to write a fairly large amount of code
> for what seems like a simple operation. I was wondering if anyone could
> give me some advice on setting up some sort of loop function to step
> through each z-coordinate and extract the appropriate 720x360 grid and
> then save this grid in the form of a text file. It is neccessary that
> each grid has a different name. I had a read around and think that a
> "for" loop would do the job, i.e. something in the form of...
> 
> for (i in 1:132)
> a <- clim[1:720, 1:360, i]
> 
> ...and then use the "write.table" function to save my grid as a text
> file. However, my problem is that I'm not sure how to structure my loop
> so that each 720x360 grid will be given a different name. I'm also
> unaware as to how to incorporate my different names into "write.table".
> Is it possible to set up a loop for this function and again step
> through each file and save them separately? Or should I use a different
> function to save my grids?
> 
> Thanks in advance for your help.
> 
> Andy
> 
> 
> 
> _________________________________________________________________
> 
> icons.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.




More information about the R-help mailing list