i want to compare two variables (having numbers) serially for matching/dismatching both having equal length> x[1] 2 2 1 2 3 2 2 2 2 3 3 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 3 2 3 2 2 2 1 2 2 [38] 2 2 2 3 2 3 2 2 1 2 2 2 2 1 1 1 3 2 2 2 2 1 1 3 1 1 1 2 2 2 3 2 3 3 3 2 2 [75] 2 2 3 2 2 1 1 1 2 3 2 2 2 2 2 2 2 2 2 1 1 3 2 2 2 2 2 2 2 2 2 2 2 1 3> y[1] 2 2 2 1 3 1 3 2 1 3 3 3 3 2 2 1 2 3 3 3 1 1 2 2 2 2 2 2 1 2 3 1 2 2 1 1 2 [38] 1 2 3 2 1 3 1 1 2 2 2 2 1 2 1 2 3 2 3 2 1 2 2 3 1 1 1 2 2 2 3 2 3 3 3 3 3 [75] 2 2 1 2 1 2 1 2 1 3 3 3 1 2 2 2 1 2 1 2 2 3 2 3 2 3 2 2 2 2 1 2 2 2 3> length(x)[1] 109> length(y)[1] 109> #both having equal length > which(x%in%y) #gives no of matches positions[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 [91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 [109] 109> length(which(x%in%y))[1] 109 it show that all numbers are matched ,but in real this is not true so,how to find matched numbers from two variables(numeric) like this? -- View this message in context: http://r.789695.n4.nabble.com/problem-in-matching-numbers-in-two-variables-tp4594912p4594912.html Sent from the R help mailing list archive at Nabble.com.
Hello, Why not == ? x == y which(x == y) Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/problem-in-matching-numbers-in-two-variables-tp4594912p4594976.html Sent from the R help mailing list archive at Nabble.com.
On 28-04-2012, at 17:28, sagarnikam123 wrote:> i want to compare two variables (having numbers) serially for > matching/dismatching > both having equal length > >> x > [1] 2 2 1 2 3 2 2 2 2 3 3 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 3 2 3 2 2 2 1 > 2 2 > [38] 2 2 2 3 2 3 2 2 1 2 2 2 2 1 1 1 3 2 2 2 2 1 1 3 1 1 1 2 2 2 3 2 3 3 3 > 2 2 > [75] 2 2 3 2 2 1 1 1 2 3 2 2 2 2 2 2 2 2 2 1 1 3 2 2 2 2 2 2 2 2 2 2 2 1 3 >> y > [1] 2 2 2 1 3 1 3 2 1 3 3 3 3 2 2 1 2 3 3 3 1 1 2 2 2 2 2 2 1 2 3 1 2 2 1 > 1 2 > [38] 1 2 3 2 1 3 1 1 2 2 2 2 1 2 1 2 3 2 3 2 1 2 2 3 1 1 1 2 2 2 3 2 3 3 3 > 3 3 > [75] 2 2 1 2 1 2 1 2 1 3 3 3 1 2 2 2 1 2 1 2 2 3 2 3 2 3 2 2 2 2 1 2 2 2 3 >> length(x) > [1] 109 >> length(y) > [1] 109 >> #both having equal length >> which(x%in%y) #gives no of matches positions > [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 > 18 > [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 > 36 > [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 > 54 > [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 > 72 > [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 > 90 > [91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 > 108 > [109] 109 >> length(which(x%in%y)) > [1] 109 > > it show that all numbers are matched ,but in real this is not true > so,how to find matched numbers from two variables(numeric) like this?Please use dput() in future. It's not quite clear what you exactly want but maybe this will do x == y which(x==y) Berend
I think this works bu there probably is a better way. xy <- x %% y which(xy != 0) John Kane Kingston ON Canada> -----Original Message----- > From: sagarnikam123 at gmail.com > Sent: Sat, 28 Apr 2012 08:28:39 -0700 (PDT) > To: r-help at r-project.org > Subject: [R] problem in matching numbers in two variables > > i want to compare two variables (having numbers) serially for > matching/dismatching > both having equal length > >> x > [1] 2 2 1 2 3 2 2 2 2 3 3 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 3 2 3 2 2 2 > 1 > 2 2 > [38] 2 2 2 3 2 3 2 2 1 2 2 2 2 1 1 1 3 2 2 2 2 1 1 3 1 1 1 2 2 2 3 2 3 3 > 3 > 2 2 > [75] 2 2 3 2 2 1 1 1 2 3 2 2 2 2 2 2 2 2 2 1 1 3 2 2 2 2 2 2 2 2 2 2 2 1 > 3 >> y > [1] 2 2 2 1 3 1 3 2 1 3 3 3 3 2 2 1 2 3 3 3 1 1 2 2 2 2 2 2 1 2 3 1 2 2 > 1 > 1 2 > [38] 1 2 3 2 1 3 1 1 2 2 2 2 1 2 1 2 3 2 3 2 1 2 2 3 1 1 1 2 2 2 3 2 3 3 > 3 > 3 3 > [75] 2 2 1 2 1 2 1 2 1 3 3 3 1 2 2 2 1 2 1 2 2 3 2 3 2 3 2 2 2 2 1 2 2 2 > 3 >> length(x) > [1] 109 >> length(y) > [1] 109 >> #both having equal length >> which(x%in%y) #gives no of matches positions > [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 > 18 > [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 > 36 > [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 > 54 > [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 > 72 > [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 > 90 > [91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 > 108 > [109] 109 >> length(which(x%in%y)) > [1] 109 > > it show that all numbers are matched ,but in real this is not true > so,how to find matched numbers from two variables(numeric) like this? > > -- > View this message in context: > http://r.789695.n4.nabble.com/problem-in-matching-numbers-in-two-variables-tp4594912p4594912.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
No its not perfectly working see below> j<-c(1,1,2,2,3) > k<-c(2,1,1,1,1) > j%%k[1] 1 0 0 0 0 problem comes when one of my sequence have 1 -- View this message in context: http://r.789695.n4.nabble.com/problem-in-matching-numbers-in-two-variables-tp4594912p4595849.html Sent from the R help mailing list archive at Nabble.com.
I think I misread the question. Is this what you want j <- c(1,1,2,2,3) k <- c(2,1,1,1,1) xx <- j == k which(xx == TRUE) John Kane Kingston ON Canada> -----Original Message----- > From: sagarnikam123 at gmail.com > Sent: Sat, 28 Apr 2012 23:48:43 -0700 (PDT) > To: r-help at r-project.org > Subject: Re: [R] problem in matching numbers in two variables > > No its not perfectly working see below > >> j<-c(1,1,2,2,3) >> k<-c(2,1,1,1,1) >> j%%k > [1] 1 0 0 0 0 > > problem comes when one of my sequence have 1 > > -- > View this message in context: > http://r.789695.n4.nabble.com/problem-in-matching-numbers-in-two-variables-tp4594912p4595849.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
On 29-04-2012, at 08:48, sagarnikam123 wrote:> No its not perfectly working see below > >> j<-c(1,1,2,2,3) >> k<-c(2,1,1,1,1) >> j%%k > [1] 1 0 0 0 0 > > problem comes when one of my sequence have 1Did you try the other solutions that were suggested? Berend
Reasonably Related Threads
- iterating through for loop
- how to append element at last position in array dynamically
- how to avoid writing index in write.table command
- Is it there any std pattern in R which show same representation style of ppt found on net
- how to check given number seq. is time series or not?