I have a matrix of lists. Something along the lists of (but much bigger than): x = array(dim=c(2, 2), data=list()) x[1,1] = list(1:5) x[2,1] = list(6:9) x[1,2] = list(10:13) x[2,2] = list(14:16) Each list contains a number of observations/ground truth for a particular state. That is, state <1,1> has observations <1,2,3,4,5>. States may have a different number of observations (which is why I'm not using an array). I have another numeric matrix with the same dimensions and I want to check if its values occur. Something along the lines of: y = array(dim=c(2, 2), data=c(1, 10, 11, 20)) I want to do: y %in% x But it doesn't work as hoped. (I expect: c(T, F, T, F).) I realize that I can do this with sapply, but I was hoping for a faster / smarter solution. Any ideas? Thanks! :) Neal
Bottom line: No, I dont see any vectorized way to do this. However, the following may offer some slight improvement over your approach. 1. Why do you need to store these as arrays, which are merely vectors with a "dim" attribute? ## convert to vectors (a list is also a vector): dim(x) <- NULL; dim(y) <- NULL 2. ... and use mapply() instead of sapply() (not clear to me *how* you mean to use sapply() anyway)> mapply("%in%",y,x) ## might be slightly faster to use match() directly[1] TRUE FALSE TRUE FALSE ## NOT c(T,F,T,F) Whether this makes any noticeable difference seems doubtful, however. There may be special packages that do offer a real improvement, so you should search. rseek.org is a good R search engine, although google often does well also. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Jul 30, 2016 at 9:58 AM, Neal H. Walfield <neal at walfield.org> wrote:> I have a matrix of lists. Something along the lists of (but much > bigger than): > > x = array(dim=c(2, 2), data=list()) > x[1,1] = list(1:5) > x[2,1] = list(6:9) > x[1,2] = list(10:13) > x[2,2] = list(14:16) > > Each list contains a number of observations/ground truth for a > particular state. That is, state <1,1> has observations <1,2,3,4,5>. > States may have a different number of observations (which is why I'm > not using an array). > > I have another numeric matrix with the same dimensions and I want to > check if its values occur. Something along the lines of: > > y = array(dim=c(2, 2), data=c(1, 10, 11, 20)) > > I want to do: > > y %in% x > > But it doesn't work as hoped. (I expect: c(T, F, T, F).) > > I realize that I can do this with sapply, but I was hoping for a > faster / smarter solution. Any ideas? > > Thanks! > > :) Neal > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
On Sat, 30 Jul 2016 19:28:42 +0200, Bert Gunter wrote:> Bottom line: No, I dont see any vectorized way to do this. > > However, the following may offer some slight improvement over your approach. > > 1. Why do you need to store these as arrays, which are merely vectors > with a "dim" attribute? > > ## convert to vectors (a list is also a vector): > > dim(x) <- NULL; dim(y) <- NULLI apologize that my example was not minimal. I have the data stored in an array for other reasons.> 2. ... and use mapply() instead of sapply() (not clear to me *how* > you mean to use sapply() anyway)Sorry, I meant (and I'm using) mapply.> > mapply("%in%",y,x) ## might be slightly faster to use match() directly > > [1] TRUE FALSE TRUE FALSE ## NOT c(T,F,T,F)I'm not sure what you mean by NOT here. You get the same answer as I do, as far as I can see. Thanks for your help! :) Neal
Hello, I don't have a solution but see the difference between the two loops below. for(j in 1:2) ?? ?for(i in 1:2){ ?? ??? ?cat(y[i,j], "\n") ?? ??? ?cat(x[i,j][[1]], "\n") ?? ??? ?cat(y[i,j] %in% x[i,j], "\n", "\n") ?? ?} for(j in 1:2) ?? ?for(i in 1:2){ ?? ??? ?cat(y[i,j], "\n") ?? ??? ?cat(x[i,j][[1]], "\n") ?? ??? ?cat(y[i,j] %in% x[i,j][[1]], "\n", "\n") ?? ?} So apparently y %in% x is equivalent to the first, but you want the second. Note that the following doesn't work either. y %in% x[[1]] [1]? TRUE FALSE FALSE FALSE Rui Barradas ? Citando Neal H. Walfield <neal at walfield.org>:> I have a matrix of lists.? Something along the lists of (but much > bigger than): > > x = array(dim=c(2, 2), data=list()) > x[1,1] = list(1:5) > x[2,1] = list(6:9) > x[1,2] = list(10:13) > x[2,2] = list(14:16) > > Each list contains a number of observations/ground truth for a > particular state.? That is, state <1,1> has observations <1,2,3,4,5>. > States may have a different number of observations (which is why I'm > not using an array). > > I have another numeric matrix with the same dimensions and I want to > check if its values occur.? Something along the lines of: > > y = array(dim=c(2, 2), data=c(1, 10, 11, 20)) > > I want to do: > > y %in% x > > But it doesn't work as hoped.? (I expect: c(T, F, T, F).) > > I realize that I can do this with sapply, but I was hoping for a > faster / smarter solution.? Any ideas? > > Thanks! > > :) Neal > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.htmland provide commented, > minimal, self-contained, reproducible code.? [[alternative HTML version deleted]]