Folks, I have Googled but not found much regarding arrayInd aside from the "which" help page. Any good examples or docs on what arrayInd does that is better or different from which()? In addition take the following 20x10 matrix: td<-structure(c(1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 5, 6, 6, 6, 6, 5, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6), .Dim = c(20L, 10L )) I want to find the cells which (hah!) are <= c(rep(5,5), rep(4,5)). That is my bounds are by column. Is there a better way to do this other than: bounds<-c(rep(5,5), rep(4,5)) idxs<-which(apply(td, 2, "<=", bounds), arr.ind = TRUE)> idxsrow col [1,] 1 1 [2,] 13 1 [3,] 13 2 [4,] 1 3 [5,] 8 3 [6,] 13 3 [7,] 1 4 [8,] 13 4 [9,] 1 5 [10,] 13 5 [11,] 1 6 [12,] 4 6 [13,] 13 6 [14,] 4 7 [15,] 13 7 [16,] 1 8 [17,] 4 8 [18,] 13 8 [19,] 3 9 [20,] 1 10 [21,] 13 10 Lastly can you explain these results:> td[idxs[10,]][1] 4 6> td[idxs[10,1]][1] 4> td[idxs[10,2]][1] 6> td[idxs[10,3]]Error: subscript out of bounds Thanks in advance for your help, KW --
KW, I don't know anything about arrayInd, so I can't help with that question. In order to determine if there is a better way to do your comparison depends largely on what you want to do with the results. The way that you're doing it now seems fine to me, but I wonder what you want to use the indexes for. You could approach the issue from another angle, which preserves the original matrix structure. compare <- cbind(td[, 1:5]<=5, td[, 6:10]<=4) compare td[compare] td2 <- td td2[!compare] <- NA td2 You are having difficulty extracting. The information that you are providing are all vectors and scalars. td[idxs[10,]] td[c(13, 5)] td[idxs[10,1]] td[13] If you want to extract by row and column, you have to provide matrices. td[idxs[10, , drop=FALSE]] td[t(matrix(c(13, 5)))] The error in your last attempt is caused by trying to extract row 10 column 3 from a matrix (idxs) that has only 2 columns. td[idxs[10,3]] idxs[10,3] dim(idxs) Hope this helps. Jean On Wed, Apr 3, 2013 at 9:53 AM, Keith S Weintraub <kw1958@gmail.com> wrote:> Folks, > > I have Googled but not found much regarding arrayInd aside from the > "which" help page. > > Any good examples or docs on what arrayInd does that is better or > different from which()? > > In addition take the following 20x10 matrix: > > td<-structure(c(1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, > 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, > 6, 6, 1, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, > 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, > 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 2, > 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 5, 6, 6, 6, 6, 5, 6, > 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, > 4, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, > 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, > 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6), .Dim = c(20L, 10L > )) > > I want to find the cells which (hah!) are <= c(rep(5,5), rep(4,5)). That > is my bounds are by column. > > Is there a better way to do this other than: > > bounds<-c(rep(5,5), rep(4,5)) > idxs<-which(apply(td, 2, "<=", bounds), arr.ind = TRUE) > > > idxs > row col > [1,] 1 1 > [2,] 13 1 > [3,] 13 2 > [4,] 1 3 > [5,] 8 3 > [6,] 13 3 > [7,] 1 4 > [8,] 13 4 > [9,] 1 5 > [10,] 13 5 > [11,] 1 6 > [12,] 4 6 > [13,] 13 6 > [14,] 4 7 > [15,] 13 7 > [16,] 1 8 > [17,] 4 8 > [18,] 13 8 > [19,] 3 9 > [20,] 1 10 > [21,] 13 10 > > Lastly can you explain these results: > > > td[idxs[10,]] > [1] 4 6 > > > td[idxs[10,1]] > [1] 4 > > > td[idxs[10,2]] > [1] 6 > > > td[idxs[10,3]] > Error: subscript out of bounds > > Thanks in advance for your help, > KW > > -- > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
On Apr 3, 2013, at 7:53 AM, Keith S Weintraub wrote:> Folks, > > I have Googled but not found much regarding arrayInd aside from the "which" help page. > > Any good examples or docs on what arrayInd does that is better or different from which()? > > In addition take the following 20x10 matrix: > > td<-structure(c(1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, > 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, > 6, 6, 1, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, > 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, > 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 2, > 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 5, 6, 6, 6, 6, 5, 6, > 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, > 4, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, > 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, > 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6), .Dim = c(20L, 10L > )) > > I want to find the cells which (hah!) are <= c(rep(5,5), rep(4,5)). That is my bounds are by column. > > Is there a better way to do this other than: > > bounds<-c(rep(5,5), rep(4,5)) > idxs<-which(apply(td, 2, "<=", bounds), arr.ind = TRUE) > >> idxs > row col > [1,] 1 1 > [2,] 13 1 > [3,] 13 2 > [4,] 1 3 > [5,] 8 3 > [6,] 13 3 > [7,] 1 4 > [8,] 13 4 > [9,] 1 5 > [10,] 13 5 > [11,] 1 6 > [12,] 4 6 > [13,] 13 6 > [14,] 4 7 > [15,] 13 7 > [16,] 1 8 > [17,] 4 8 > [18,] 13 8 > [19,] 3 9 > [20,] 1 10 > [21,] 13 10 > > Lastly can you explain these results: > >> td[idxs[10,]] > [1] 4 6 > >> td[idxs[10,1]] > [1] 4 > >> td[idxs[10,2]] > [1] 6 > >> td[idxs[10,3]] > Error: subscript out of boundsThis has nothing to do with the behavior of arrayInd and everything to do with the behavior of "[".> td[idxs[10,drop=FALSE] ][1] 4 When extracting from a matrix with a result of asingle row the extracted object looses its matrix attributes and becomes a numeric vector. That behavior is prevented with drop=FALSE and desire results accrue. This would not have been a puzzle if you had chose multiple rows at a time:> td [ idxs[1:2, ] ][1] 1 4> td [ idxs ][1] 1 4 1 1 3 3 1 5 3 4 2 1 1 5 3 2 2 4 1 2 3 3 -- David Winsemius Alameda, CA, USA
On Apr 3, 2013, at 10:59 AM, David Winsemius wrote:> > On Apr 3, 2013, at 7:53 AM, Keith S Weintraub wrote: > >> Folks, >> >> I have Googled but not found much regarding arrayInd aside from the "which" help page. >> >> Any good examples or docs on what arrayInd does that is better or different from which()? >> >> In addition take the following 20x10 matrix: >> >> td<-structure(c(1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, >> 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, >> 6, 6, 1, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, >> 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, >> 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 2, >> 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 5, 6, 6, 6, 6, 5, 6, >> 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, >> 4, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, >> 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, >> 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6), .Dim = c(20L, 10L >> )) >> >> I want to find the cells which (hah!) are <= c(rep(5,5), rep(4,5)). That is my bounds are by column. >> >> Is there a better way to do this other than: >> >> bounds<-c(rep(5,5), rep(4,5)) >> idxs<-which(apply(td, 2, "<=", bounds), arr.ind = TRUE) >> >>> idxs >> row col >> [1,] 1 1 >> [2,] 13 1 >> [3,] 13 2 >> [4,] 1 3 >> [5,] 8 3 >> [6,] 13 3 >> [7,] 1 4 >> [8,] 13 4 >> [9,] 1 5 >> [10,] 13 5 >> [11,] 1 6 >> [12,] 4 6 >> [13,] 13 6 >> [14,] 4 7 >> [15,] 13 7 >> [16,] 1 8 >> [17,] 4 8 >> [18,] 13 8 >> [19,] 3 9 >> [20,] 1 10 >> [21,] 13 10 >> >> Lastly can you explain these results: >> >>> td[idxs[10,]] >> [1] 4 6 >> >>> td[idxs[10,1]] >> [1] 4 >> >>> td[idxs[10,2]] >> [1] 6 >> >>> td[idxs[10,3]] >> Error: subscript out of bounds > > This has nothing to do with the behavior of arrayInd and everything to do with the behavior of "[". > >> td[idxs[10,drop=FALSE] ] > [1] 4Arrgh. The explanation was correct, but there is a missing comma. Should be: td[idxs[10, ,drop=FALSE] ] Only shows up if you chose a different row> td [ idxs[12, drop=FALSE] ][1] 6 WRONG> td [ idxs[12, , drop=FALSE] ][1] 1 Correct -- David.> When extracting from a matrix with a result of asingle row the extracted object looses its matrix attributes and becomes a numeric vector. That behavior is prevented with drop=FALSE and desire results accrue. > > This would not have been a puzzle if you had chose multiple rows at a time: > >> td [ idxs[1:2, ] ] > [1] 1 4 >> td [ idxs ] > [1] 1 4 1 1 3 3 1 5 3 4 2 1 1 5 3 2 2 4 1 2 3 3 > >David Winsemius Alameda, CA, USA