An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120201/24c2a200/attachment.pl>
Selecting contiguous, irregularly-shaped sets of values from arrays
2 messages · Chris Campbell, Bryan McCloskey
5 days later
Chris,
That worked exceptionally well. Here's the pseudo-code for what I ended up using:
library(spatstat)
depth_bin<-depth<-stage-elevation #depth at grid cells = stage matrix - elevation matrix; initialize binary matrix
depth_bin[,]<-depth>5 #binary matrix of cells >5cm deep
depth_con_im<-connected(as.im(depth_bin),background=0) #8-connected regions of pixel image
depth_con<-depth_con_im$v #v element is data matrix
class(depth_con)<-"matrix"
for (i in 1:length(coords$points)) { #points of interest
x_co<-which.min(abs(x-coords$EASTING[i]))
y_co<-which.min(abs(y-coords$NORTHING[i]))
pond[i]<-depth_con[x_co,y_co] #pond of cell of interest
area[i]<-length(which(depth_con==pond[i]))*400*400 #area of the pond (400m grid)
depth[i]<-depth[x_co,y_co] #depth at cell of interest
}
Thanks for the tip!
-bryan
------
Bryan McCloskey, Ph.D.
IT Specialist (Data Management/Internet)
U.S. Geological Survey
St. Petersburg Coastal & Marine Science Center
600 Fourth St. South
St. Petersburg, FL 33701
South Florida Information Access: http://sofia.usgs.gov
Everglades Depth Estimation Network: http://sofia.usgs.gov/eden
Phone: 727.803.8747x3017 * Fax: 727.803.2032
------
On Feb 1, 2012, at 9:34 AM, Chris Campbell wrote:
Dear Bryan, You could try using spatial techniques to choose the contiguous areas of your matrices.
require(spatstat) set.seed(1520) x <- matrix(rnorm(25), nrow=5, ncol=5,
+ dimnames=list(c("A","B","C","D","E"), c("v","w","x","y","z")))
x
v w x y z A 0.3089046 -0.003350135 -0.4506777 0.7971787 -1.95078919 B -1.5895009 0.336233539 -0.3237293 0.7676754 -0.76756928 C -1.0324022 -1.119037223 -1.1525350 0.6057773 -0.28930702 D -0.8440912 -0.499994418 0.7664473 0.6367184 -0.09801227 E 0.6261038 0.391232210 0.4967601 1.0753439 -0.50998559
z <- y <- x y[, ] <- x < 0.6 y
v w x y z A 1 1 1 0 1 B 1 1 1 0 1 C 1 1 1 0 1 D 1 1 0 0 1 E 0 1 1 0 1
yi <- as.im(y) ycOut <- connected(yi, background = 0) yc <- ycOut$v yc
[,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 <NA> 2 [2,] 1 1 1 <NA> 2 [3,] 1 1 1 <NA> 2 [4,] 1 1 <NA> <NA> 2 [5,] <NA> 1 1 <NA> 2 Levels: 1 2
z[yc != 1 | is.na(yc)] <- NA z
v w x y z A 0.3089046 -0.003350135 -0.4506777 NA NA B -1.5895009 0.336233539 -0.3237293 NA NA C -1.0324022 -1.119037223 -1.1525350 NA NA D -0.8440912 -0.499994418 NA NA NA E NA 0.391232210 0.4967601 NA NA Hope this helps. Best wishes, Chris Campbell MANGO SOLUTIONS Data Analysis that Delivers +44 1249 767700 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bryan McCloskey Sent: 31 January 2012 19:58 To: r-help at r-project.org Subject: [R] Selecting contiguous, irregularly-shaped sets of values from arrays All, I am attempting to select all of the contiguous elements of a matrix that meet some criterion. I.e., values that would be contained within an irregular area defined by a "contour" applied around point of interest. So, if I have a matrix x as follows:
x <- matrix(rnorm(25), nrow=5, ncol=5,
dimnames=list(c("A","B","C","D","E"), c("v","w","x","y","z"))
x
v w x y z A 0.5184795 1.9641285 0.8632044 1.5010397 0.8468490 B -1.2402866 0.5211307 -0.1474351 1.3264893 0.1087390 C 0.5910275 -1.1708906 0.9440755 1.0970971 -0.2784806 D 0.6377495 1.1594035 -0.4217621 1.4021680 -0.6487677 E -1.4590833 0.2065765 0.1623669 1.3598283 0.3742821
how can I select all values in the "pond" of contiguous matrix entries that have values, say <0.6, if my entry of interest is x["A","v"]. In that case, I would like to select the following starred entries:
v w x y z
A 0.5184795* 1.9641285 0.8632044 1.5010397 0.8468490 B -1.2402866* 0.5211307* -0.1474351* 1.3264893 0.1087390 C 0.5910275* -1.1708906* 0.9440755 1.0970971 -0.2784806 D 0.6377495 1.1594035 -0.4217621* 1.4021680 -0.6487677 E -1.4590833 0.2065765* 0.1623669* 1.3598283 0.3742821
But I would _not_ like to select any of the values in x[,"z"], because, even though they may be <0.6, they are not contiguous with the pond that x["A","v"] is in.
Is there an easy way to do this for many points of interest in a large matrix?
Thanks,
-bryan
------
Bryan McCloskey, Ph.D.
U.S. Geological Survey
St. Petersburg Coastal & Marine Science Center St. Petersburg
------
______________________________________________ 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. LEGAL NOTICE This message is intended for the use of the named recipient(s) only and may contain confidential and / or privileged information. If you are not the intended recipient, please contact the sender and delete this message. Any unauthorised use of the information contained in this message is prohibited. Mango Business Solutions Limited is registered in England under No. 4560258 with its registered office at Suite 3, Middlesex House, Rutherford Close, Stevenage, Herts, SG1 2EF, UK. PLEASE CONSIDER THE ENVIRONMENT BEFORE PRINTING THIS EMAIL