Hi Robert, Thanks for the clarification. Yes the gridcells.txt is regular spaced points at 0.0025 degree resolution. I was able to run the code, but extracted the z values instead of x and y values. Is it possible to extract them? Thanks, Jin ________________________________ From: Robert J. Hijmans [mailto:r.hijmans@gmail.com] Sent: Thursday, 4 October 2012 2:32 PM To: Li Jin Cc: r-sig-geo@r-project.org Subject: Re: [R-sig-Geo] Extract location information from grid cells for sample points [SEC=UNCLASSIFIED] On Wed, Oct 3, 2012 at 9:20 PM, > wrote: Hi Robert, Thank you very much for the reply. Both datasets are in txt format. Lat and long are in WGS84. So I might need to do something like: library(raster) p <- read.table('points.txt') d <- read.table('gridcells.txt') r <- rasterFromPoints(d) values<- extract(r, p) Is anything else I should add or modify? rasterFromPoints seems not available in raster 2.0-05. Sorry, I misspoke, I should have said "rasterFromXYZ" I still do not know what "d" looks like. "txt format" is not very descriptive, but if it regular spaced points, something like x, y, z 1, 2, 3, 1, 3, 5 then you can probably do what you write above, using rasterFromXYZ instead of rasterFromPoints Robert What I intend to do is extract and add lat/long of corresponding grid cells to my points dataset as below: > points lat.sample long.sample lat.gridcell long.gridcell 1 -21.258000 110.502000 2 -42.609683 144.412700 3 -36.733000 132.050000 4 -21.330000 110.482000 5 -13.156840 136.757080 6 -14.633000 117.967000 7 -23.502580 111.083450 8 -15.674870 138.058400 9 -12.650000 136.950000 10 -9.955000 143.806667 All the best, Jin ________________________________ From: Robert J. Hijmans [mailto:r.hijmans@gmail.com] Sent: Thursday, 4 October 2012 1:13 PM To: Li Jin Cc: r-sig-geo@r-project.org Subject: Re: [R-sig-Geo] Extract location information from grid cells for sample points [SEC=UNCLASSIFIED] Hi Jin, >From your description it is not entirely clear what your input data looks like. But you can something like this library(raster) p <- read.table('points.txt') r <- raster('file.asc') values<- extract(r, p) Perhaps you need to subset "p" to get the appropriate columns representing longitude and latitude (in that order). Or perhaps your grid data is not really in a 'raster' format? I am not sure from your description. In that case you may need to to d <- read.table('file.asc') r <- rasterFromPoints(d) You may also want to check out functions cellFromXY to link coordinates to cells in a raster. All the best, Robert On Wed, Oct 3, 2012 at 5:38 PM, > wrote: Dear list, I have two datasets: one is for point samples with lat and long information at accuracy of 0.001 degrees. Another is grid cells at a resolution of 0.0025 degrees. Both datasets are in txt format. Now I need to extract the lat and long information from the grid cell dataset for the point samples by assigning the lat and long of a grid cell to a sample point that falls in the grid cell. I usually get this done in ArcGIS. I was wondering if it is possible in R. I searched but failed to find a right function to implement this in R. Many thanks for any suggestions. Regards, Jin Li Spatial Modeller/Computational Statistician | Coastal, Marine and Climate Change Group Environmental Geoscience Division | GEOSCIENCE AUSTRALIA ____________________________________________________________ Phone: +61 2 6249 9899 Fax: +61 2 6249 9999 Email: Jin.Li@ga.gov.au> Web: www.ga.gov.au Cnr Jerrabomberra Avenue and Hindmarsh Drive Symonston ACT GPO Box 378 Canberra ACT 2601 Australia Applying geoscience to Australia's most important challenges Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. ------------------------------------------------------------------------------------------------------------------------- [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. ------------------------------------------------------------------------------------------------------------------------- Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. ------------------------------------------------------------------------------------------------------------------------- [[alternative HTML version deleted]]