[R] matching vectors
jim holtman
jholtman at gmail.com
Tue Mar 17 16:12:25 CET 2009
You can use something like this to make sure that h2 is a subset of h1
in consecutive order:
> h1 <- 1:50
> h2 <- 25:30
> hm <- match(h2, h1)
> all(diff(hm) == 1)
[1] TRUE
>
On Tue, Mar 17, 2009 at 6:42 AM, emj83 <stp08emj at shef.ac.uk> wrote:
>
> I have two numerical vectors; hun2 is a subset of hun1:
>
>> hun1
> [1] 147 198 141 170 184 136 150 134 144 161 124 103 107 110 121 79 106
> 51
> [19] 102 109 52 71 52 80 95 78 75 96 101 107 107 80 71 49 71
> 64
> [37] 58 68 75 85 55 52 70 90 98 90 100 98 74 78 60 69 106
> 103
> [55] 106 124 111 97 66 83 54 58 74 76 73 101 68 50 45 69 69
> 75
> [73] 80 55 79 58 61 72 90 91 99 58 63 57 72 36 58 44 63
> 54
> [91] 39 41 48 65 86 73 74 69 66 59 52 68 91 100 151 112 119
> 93
> [109] 66 73 87 99 79 109 94 80 63 51 46 39 28 34 49 45 36
> 32
> [127] 36 39 52 63 59 47 57 52 46 39 54 53 80 43 48 37 42
> 43
> [145] 47 34 42 45 50 48 49 55 62 46 61 60 42 48 58 49 60
>
>>hun2
> [1] 151 112 119 93 66 73 87 99 79 109 94 80 63 51 46 39 28 34
> 49
> [20] 45 36 32 36 39 52 63 59 47 57 52 46 39 54 53 80 43 48
> 37
> [39] 42 43 47 34 42 45 50 48 49 55 62 46 61 60 42 48 58 49
> 60
>
> i want to cbind hun1 to hun2 at the position where they match.
>
> i have attempted to start this by using pmatch() to identify where it is
> they match- i get the following results.
>
>> pmatch(hun1,hun2,duplicates.ok=TRUE)
> [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 9 NA 14 NA 10 25 NA 25
> 12 NA
> [26] NA NA NA NA NA NA 12 NA 19 NA NA 55 NA NA NA 48 25 NA NA NA NA NA NA
> NA NA
> [51] 52 NA NA NA NA NA NA NA 5 NA 33 55 NA NA 6 NA NA 45 20 NA NA NA 12
> 48 9
> [76] 55 51 NA NA NA 8 55 13 29 NA 21 55 NA 13 33 16 NA 37 NA NA 6 NA NA
> 5 27
> [101] 25 NA NA NA 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> 20 21
> [126] 22 21 16 25 13 27 28 29 25 15 16 33 34 12 36 37 38 39 36 28 18 39 20
> 45 37
> [151] 19 48 49 15 51 52 39 37 55 19 52
>
> is there anyway that i can specify pmatch() to work if and only if it
> identifies all of hun2: something like this???
> [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
> NA NA
> [26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
> NA NA
> [51] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
> NA NA
> [76] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
> NA NA
> [101] NA NA NA NA NA NA NA NA NA 1 2 3 4 5 6 7 8 9 10 11 12 13 14
> 15 16
> [126] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
> 40 41
> [151] 42 43 44 45 46 47 48 49 50 51 52
>
>
>
> --
> View this message in context: http://www.nabble.com/matching-vectors-tp22556470p22556470.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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list