Bryan McCloskey
2012-Jan-31 19:58 UTC
[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"))> xv 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 ------
Chris Campbell
2012-Feb-01 14:34 UTC
[R] Selecting contiguous, irregularly-shaped sets of values from arrays
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")))> xv 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 > yv 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 > zv 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@r-project.org [mailto:r-help-bounces@r-project.org] On Behalf Of Bryan McCloskey Sent: 31 January 2012 19:58 To: r-help@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"))> xv 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@r-project.org<mailto:R-help@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...{{dropped:14}}