How to match two string vectors if the strings case is different in both the vectors in R?

R ProgrammingServer Side ProgrammingProgramming

<p style="">We know that, R is a case sensitive programming language, hence matching strings of different case is not simple. For example, if a vector contains tutorialspoint and the other contains TUTORIALSPOINT then to check whether the strings match or not, we cannot use match function directly. To do this, we have to convert the lowercase string to uppercase or uppercase to lowercase with the match function.</p><h2>Examples</h2><p><a class="demo" href="http://tpcg.io/ls9yBjbV" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">&gt; x1&lt;-sample(letters[1:26],100,replace=TRUE) &gt; x1</pre><h2>Output</h2><pre class="result notranslate">[1] &quot;z&quot; &quot;v&quot; &quot;r&quot; &quot;y&quot; &quot;z&quot; &quot;l&quot; &quot;v&quot; &quot;t&quot; &quot;f&quot; &quot;p&quot; &quot;p&quot; &quot;z&quot; &quot;e&quot; &quot;b&quot; &quot;a&quot; &quot;o&quot; &quot;m&quot; &quot;d&quot; [19] &quot;e&quot; &quot;l&quot; &quot;y&quot; &quot;y&quot; &quot;u&quot; &quot;u&quot; &quot;w&quot; &quot;b&quot; &quot;a&quot; &quot;j&quot; &quot;n&quot; &quot;v&quot; &quot;b&quot; &quot;q&quot; &quot;b&quot; &quot;d&quot; &quot;l&quot; &quot;a&quot; [37] &quot;g&quot; &quot;g&quot; &quot;g&quot; &quot;o&quot; &quot;k&quot; &quot;r&quot; &quot;q&quot; &quot;e&quot; &quot;x&quot; &quot;i&quot; &quot;r&quot; &quot;l&quot; &quot;b&quot; &quot;r&quot; &quot;j&quot; &quot;k&quot; &quot;b&quot; &quot;f&quot; [55] &quot;r&quot; &quot;f&quot; &quot;r&quot; &quot;n&quot; &quot;y&quot; &quot;y&quot; &quot;l&quot; &quot;k&quot; &quot;y&quot; &quot;s&quot; &quot;b&quot; &quot;a&quot; &quot;s&quot; &quot;f&quot; &quot;a&quot; &quot;l&quot; &quot;j&quot; &quot;i&quot; [73] &quot;q&quot; &quot;o&quot; &quot;t&quot; &quot;v&quot; &quot;t&quot; &quot;r&quot; &quot;i&quot; &quot;x&quot; &quot;s&quot; &quot;q&quot; &quot;h&quot; &quot;t&quot; &quot;y&quot; &quot;k&quot; &quot;a&quot; &quot;h&quot; &quot;e&quot; &quot;m&quot; [91] &quot;u&quot; &quot;d&quot; &quot;q&quot; &quot;i&quot; &quot;h&quot; &quot;x&quot; &quot;k&quot; &quot;j&quot; &quot;p&quot; &quot;h&quot;</pre><h2>Example</h2><p><a class="demo" href="http://tpcg.io/gOW63oL5" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">&gt; x2&lt;-sample(LETTERS[1:10],100,replace=TRUE) &gt; x2</pre><h2>Output</h2><pre class="result notranslate">[1] &quot;E&quot; &quot;C&quot; &quot;F&quot; &quot;F&quot; &quot;A&quot; &quot;H&quot; &quot;E&quot; &quot;F&quot; &quot;D&quot; &quot;F&quot; &quot;J&quot; &quot;G&quot; &quot;G&quot; &quot;D&quot; &quot;E&quot; &quot;G&quot; &quot;G&quot; &quot;F&quot; [19] &quot;A&quot; &quot;C&quot; &quot;C&quot; &quot;H&quot; &quot;E&quot; &quot;G&quot; &quot;H&quot; &quot;A&quot; &quot;B&quot; &quot;A&quot; &quot;H&quot; &quot;G&quot; &quot;D&quot; &quot;J&quot; &quot;G&quot; &quot;C&quot; &quot;D&quot; &quot;I&quot; [37] &quot;F&quot; &quot;B&quot; &quot;D&quot; &quot;D&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot; &quot;D&quot; &quot;B&quot; &quot;E&quot; &quot;E&quot; &quot;H&quot; &quot;D&quot; &quot;D&quot; &quot;I&quot; &quot;B&quot; &quot;I&quot; &quot;J&quot; [55] &quot;C&quot; &quot;C&quot; &quot;H&quot; &quot;D&quot; &quot;B&quot; &quot;D&quot; &quot;F&quot; &quot;F&quot; &quot;D&quot; &quot;F&quot; &quot;E&quot; &quot;B&quot; &quot;F&quot; &quot;J&quot; &quot;D&quot; &quot;B&quot; &quot;G&quot; &quot;J&quot; [73] &quot;G&quot; &quot;C&quot; &quot;E&quot; &quot;A&quot; &quot;I&quot; &quot;B&quot; &quot;D&quot; &quot;A&quot; &quot;G&quot; &quot;G&quot; &quot;F&quot; &quot;D&quot; &quot;E&quot; &quot;E&quot; &quot;G&quot; &quot;I&quot; &quot;D&quot; &quot;D&quot; [91] &quot;I&quot; &quot;E&quot; &quot;J&quot; &quot;D&quot; &quot;E&quot; &quot;B&quot; &quot;C&quot; &quot;A&quot; &quot;I&quot; &quot;C&quot;</pre><pre class="prettyprint notranslate">&gt; match(x1,tolower(x2)) </pre><h2>Output</h2><pre class="result notranslate">[1] NA NA NA NA NA NA NA NA 3 NA NA NA 1 27 5 NA NA 9 1 NA NA NA NA NA NA [26] 27 5 11 NA NA 27 NA 27 9 NA 5 12 12 12 NA NA NA NA 1 NA 36 NA NA 27 NA [51] 11 NA 27 3 NA 3 NA NA NA NA NA NA NA NA 27 5 NA 3 5 NA 11 36 NA NA NA [76] NA NA NA 36 NA NA NA 6 NA NA NA 5 6 1 NA NA 9 NA 36 6 NA NA 11 NA 6</pre><pre class="prettyprint notranslate">&gt; x3&lt;-c(&quot;AK&quot;, &quot;AL&quot;, &quot;AR&quot;, &quot;AS&quot;, &quot;AZ&quot;, &quot;CA&quot;, &quot;CO&quot;, &quot;CT&quot;, &quot;DC&quot;, &quot;DE&quot;, &quot;FL&quot;, &quot;GA&quot;, &quot;GU&quot;, &quot;HI&quot;, &quot;IA&quot;, &quot;ID&quot;, &quot;IL&quot;, &quot;IN&quot;, &quot;KS&quot;, &quot;KY&quot;, &quot;LA&quot;, &quot;MA&quot;, &quot;MD&quot;, &quot;ME&quot;, &quot;MI&quot;, &quot;MN&quot;, &quot;MO&quot;, &quot;MP&quot;, &quot;MS&quot;, &quot;MT&quot;, &quot;NC&quot;, &quot;ND&quot;, &quot;NE&quot;, &quot;NH&quot;, &quot;NJ&quot;, &quot;NM&quot;, &quot;NV&quot;, &quot;NY&quot;, &quot;OH&quot;, &quot;OK&quot;, &quot;OR&quot;, &quot;PA&quot;, &quot;PR&quot;, &quot;RI&quot;, &quot;SC&quot;, &quot;SD&quot;, &quot;TN&quot;, &quot;TX&quot;, &quot;UM&quot;, &quot;UT&quot;, &quot;VA&quot;, &quot;VI&quot;, &quot;VT&quot;, &quot;WA&quot;, &quot;WI&quot;, &quot;WV&quot;, &quot;WY&quot;) &gt; x3 [1] &quot;AK&quot; &quot;AL&quot; &quot;AR&quot; &quot;AS&quot; &quot;AZ&quot; &quot;CA&quot; &quot;CO&quot; &quot;CT&quot; &quot;DC&quot; &quot;DE&quot; &quot;FL&quot; &quot;GA&quot; &quot;GU&quot; &quot;HI&quot; &quot;IA&quot; [16] &quot;ID&quot; &quot;IL&quot; &quot;IN&quot; &quot;KS&quot; &quot;KY&quot; &quot;LA&quot; &quot;MA&quot; &quot;MD&quot; &quot;ME&quot; &quot;MI&quot; &quot;MN&quot; &quot;MO&quot; &quot;MP&quot; &quot;MS&quot; &quot;MT&quot; [31] &quot;NC&quot; &quot;ND&quot; &quot;NE&quot; &quot;NH&quot; &quot;NJ&quot; &quot;NM&quot; &quot;NV&quot; &quot;NY&quot; &quot;OH&quot; &quot;OK&quot; &quot;OR&quot; &quot;PA&quot; &quot;PR&quot; &quot;RI&quot; &quot;SC&quot; [46] &quot;SD&quot; &quot;TN&quot; &quot;TX&quot; &quot;UM&quot; &quot;UT&quot; &quot;VA&quot; &quot;VI&quot; &quot;VT&quot; &quot;WA&quot; &quot;WI&quot; &quot;WV&quot; &quot;WY&quot;</pre><pre class="prettyprint notranslate">&gt; x4&lt;-c(&quot;ak&quot;, &quot;al&quot;, &quot;ar&quot;, &quot;as&quot;, &quot;az&quot;, &quot;ca&quot;, &quot;co&quot;, &quot;ct&quot;, &quot;dc&quot;, &quot;de&quot;, &quot;fl&quot;, &quot;ga&quot;, &quot;gu&quot;, &quot;hi&quot;, &quot;ia&quot;, &quot;id&quot;, &quot;il&quot;, &quot;in&quot;, &quot;ks&quot;, &quot;ky&quot;, &quot;la&quot;, &quot;ma&quot;, &quot;md&quot;, &quot;me&quot;, &quot;mi&quot;, &quot;mn&quot;, &quot;mo&quot;, &quot;mp&quot;, &quot;ms&quot;, &quot;mt&quot;, &quot;nc&quot;, &quot;nd&quot;, &quot;ne&quot;, &quot;nh&quot;, &quot;nj&quot;, &quot;nm&quot;, &quot;nv&quot;, &quot;ny&quot;, &quot;oh&quot;, &quot;ok&quot;, &quot;or&quot;, &quot;pa&quot;, &quot;pr&quot;, &quot;ri&quot;, &quot;sc&quot;, &quot;sd&quot;, &quot;tn&quot;, &quot;tx&quot;, &quot;um&quot;, &quot;ut&quot;, &quot;va&quot;, &quot;vi&quot;, &quot;vt&quot;, &quot;wa&quot;, &quot;wi&quot;, &quot;wv&quot;, &quot;wy&quot;) &gt; x4 [1] &quot;ak&quot; &quot;al&quot; &quot;ar&quot; &quot;as&quot; &quot;az&quot; &quot;ca&quot; &quot;co&quot; &quot;ct&quot; &quot;dc&quot; &quot;de&quot; &quot;fl&quot; &quot;ga&quot; &quot;gu&quot; &quot;hi&quot; &quot;ia&quot; [16] &quot;id&quot; &quot;il&quot; &quot;in&quot; &quot;ks&quot; &quot;ky&quot; &quot;la&quot; &quot;ma&quot; &quot;md&quot; &quot;me&quot; &quot;mi&quot; &quot;mn&quot; &quot;mo&quot; &quot;mp&quot; &quot;ms&quot; &quot;mt&quot; [31] &quot;nc&quot; &quot;nd&quot; &quot;ne&quot; &quot;nh&quot; &quot;nj&quot; &quot;nm&quot; &quot;nv&quot; &quot;ny&quot; &quot;oh&quot; &quot;ok&quot; &quot;or&quot; &quot;pa&quot; &quot;pr&quot; &quot;ri&quot; &quot;sc&quot; [46] &quot;sd&quot; &quot;tn&quot; &quot;tx&quot; &quot;um&quot; &quot;ut&quot; &quot;va&quot; &quot;vi&quot; &quot;vt&quot; &quot;wa&quot; &quot;wi&quot; &quot;wv&quot; &quot;wy&quot; &gt; length(x4) [1] 57 &gt; length(x3) [1] 57</pre><pre class="prettyprint notranslate">&gt; match(x3,toupper(x4)) </pre><h2>Output</h2><pre class="result notranslate">[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [51] 51 52 53 54 55 56 57</pre><pre class="prettyprint notranslate">&gt; match(LETTERS[1:20],toupper(c(letters[1:26]))) </pre><h2>Output</h2><pre class="result notranslate">[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20</pre><pre class="prettyprint notranslate">&gt; match(LETTERS[1:20],toupper(c(letters[1:10]))) </pre><h2>Output</h2><pre class="result notranslate">[1] 1 2 3 4 5 6 7 8 9 10 NA NA NA NA NA NA NA NA NA NA</pre><pre class="prettyprint notranslate">&gt; match(LETTERS[10:1],toupper(c(letters[1:10]))) </pre><h2>Output</h2><pre class="result notranslate">[1] 10 9 8 7 6 5 4 3 2 1</pre><pre class="prettyprint notranslate">&gt; match(sample(LETTERS[10:1],50,replace=TRUE),toupper(c(letters[1:10]))) </pre><h2>Output</h2><pre class="prettyprint notranslate">[1] 2 10 8 7 10 8 4 8 2 7 10 3 5 5 4 6 4 10 7 3 7 1 1 3 10 [26] 3 7 4 3 7 5 4 5 2 7 4 5 1 5 7 2 4 4 2 8 8 9 5 1 2</pre><pre class="prettyprint notranslate">&gt; match(sample(letters[26:1],50,replace=TRUE),tolower(c(LETTERS[1:20]))) </pre><h2>Output</h2><pre class="result notranslate">[1] 8 2 18 15 15 14 11 13 18 5 9 13 14 20 18 15 4 14 5 NA NA 5 NA 8 17 [26] 5 16 3 4 9 NA 5 16 17 16 6 12 1 2 NA NA 8 16 9 NA 14 NA 11 16 15</pre><pre class="prettyprint notranslate" style="">&gt; match(sample(c(&quot;india&quot;,&quot;russia&quot;,&quot;china&quot;,&quot;uk&quot;),50,replace=TRUE),tolower(c(&quot;INDIA&quot;,&quot;R USSIA&quot;,&quot;CHINA&quot;)))</pre><h2>Output</h2><pre class="result notranslate">[1] NA 3 2 1 3 1 NA NA NA 2 NA 3 3 3 1 NA NA 3 3 3 2 3 2 3 2 [26] 3 3 NA 3 3 2 NA 3 1 NA 3 NA 3 1 NA 3 NA NA NA NA 3 NA 2 NA NA</pre>
raja
Updated on 04-Sep-2020 13:06:16

Advertisements