Dear all, I was encountering with a typical Matching problem and was wondering whether R can help me to solve it directly. Let say, I have 2 vectors of equal length: vector1 <- LETTERS[1:6] vector2 <- letters[1:6] Now I need to match these 2 vectors with all possible ways like: (A,B,C,D,E) & (a,b,c,d,e) is 1 match. Another match can be (A,B,C,D,E) & (b,a,c,d,e), however there cant be any duplication. Is there any direct way to doing that in R? Thanks and regards,
Is this what you want:> vector1 <- sample(LETTERS[1:6]) # randomize > vector2 <- letters[1:6] > # convert to lower case for matching > vector1 <- tolower(vector1) > vector2 <- tolower(vector2) > # count the number of matches so order does not matter > count <- match(vector1, vector2) > if (length(count[!is.na(count)]) == length(vector1)) print("match") else print('no match')[1] "match"> vector1 <- sample(letters, 6) > vector1[1] "d" "o" "t" "z" "g" "q"> count <- match(vector1, vector2) > if (length(count[!is.na(count)]) == length(vector1)) print("match") else print('no match')[1] "no match"> >On Mon, Jul 30, 2012 at 10:55 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Dear all, I was encountering with a typical Matching problem and was > wondering whether R can help me to solve it directly. > > Let say, I have 2 vectors of equal length: > vector1 <- LETTERS[1:6] > vector2 <- letters[1:6] > > Now I need to match these 2 vectors with all possible ways like: > > (A,B,C,D,E) & (a,b,c,d,e) is 1 match. Another match can be (A,B,C,D,E) & > (b,a,c,d,e), however there cant be any duplication. > > Is there any direct way to doing that in R? > > Thanks and regards, > > ______________________________________________ > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Mon, Jul 30, 2012 at 08:40:59PM +0545, Christofer Bogaso wrote:> Dear all, I was encountering with a typical Matching problem and was > wondering whether R can help me to solve it directly. > > Let say, I have 2 vectors of equal length: > vector1 <- LETTERS[1:6] > vector2 <- letters[1:6] > > Now I need to match these 2 vectors with all possible ways like: > > (A,B,C,D,E) & (a,b,c,d,e) is 1 match. Another match can be (A,B,C,D,E) & > (b,a,c,d,e), however there cant be any duplication.Hi. If i understand correctly, all matches are obtained by taking all permutations of (a,b,c,d,e) and relating them to unchanged (A,B,C,D,E). Try the following. library(permute) vector2 <- letters[1:3] p <- allPerms(length(vector2), observed=TRUE) matrix(vector2[p], nrow=nrow(p), ncol=ncol(p)) [,1] [,2] [,3] [1,] "a" "b" "c" [2,] "a" "c" "b" [3,] "b" "a" "c" [4,] "b" "c" "a" [5,] "c" "a" "b" [6,] "c" "b" "a" The rows of the resulting matrix are all permutations of vector2. Hope this helps. Petr Savicky.
Hi, Not sure what you wanted. But, this will be just a subset of possible combinations as you described in the post. vector1<-sample(LETTERS[1:6]) ?vector2<-sample(letters[1:6]) b<-list() for(i in 1:length(vector1)){ ?b[[i]]<-list() ?b[[i]]<-sample(vector1) ?} d<-list() ?for(j in 1:length(vector2)){ ?d[[j]]<-list() ?d[[j]]<-sample(vector2) ?} as.data.frame(cbind(do.call(rbind,b),do.call(rbind,d))) ? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 1? E? F? D? C? A? B? c? f? b?? d?? e?? a 2? B? C? E? A? F? D? c? f? a?? d?? b?? e 3? D? A? E? C? B? F? b? a? f?? e?? d?? c 4? B? F? A? D? E? C? d? c? b?? f?? e?? a 5? D? B? C? E? F? A? a? c? f?? b?? d?? e 6? E? C? B? A? D? F? f? e? b?? d?? a?? c A.K. ----- Original Message ----- From: Christofer Bogaso <bogaso.christofer at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, July 30, 2012 10:55 AM Subject: [R] A "matching problem" Dear all, I was encountering with a typical Matching problem and was wondering whether R can help me to solve it directly. Let say, I have 2 vectors of equal length: vector1 <- LETTERS[1:6] vector2 <- letters[1:6] Now I need to match these 2 vectors with all possible ways like: (A,B,C,D,E) & (a,b,c,d,e) is 1 match. Another match can be (A,B,C,D,E) & (b,a,c,d,e), however there cant be any duplication. Is there any direct way to doing that in R? Thanks and regards, ______________________________________________ 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.
Does this do what you want? (using just three letters to keep the list of results short)> tmp <- expand.grid(v=letters[1:3],V=LETTERS[1:3]) > tmpv V 1 a A 2 b A 3 c A 4 a B 5 b B 6 c B 7 a C 8 b C 9 c C> > subset(tmp, tolower(tmp$V) != tmp$v)v V 2 b A 3 c A 4 a B 6 c B 7 a C 8 b C -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 7/30/12 7:55 AM, "Christofer Bogaso" <bogaso.christofer at gmail.com> wrote:>Dear all, I was encountering with a typical Matching problem and was >wondering whether R can help me to solve it directly. > >Let say, I have 2 vectors of equal length: >vector1 <- LETTERS[1:6] >vector2 <- letters[1:6] > >Now I need to match these 2 vectors with all possible ways like: > >(A,B,C,D,E) & (a,b,c,d,e) is 1 match. Another match can be (A,B,C,D,E) & >(b,a,c,d,e), however there cant be any duplication. > >Is there any direct way to doing that in R? > >Thanks and regards, > >______________________________________________ >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.