title: R: How to match or filter variables with same character strings but different sequence?
body: <p>I've a data set with two variables consisting of full names (name and surname). However, these two variables are ordered in a different sequence:</p>
  <ul>
  <li><code>variable1</code> is ordered by </li>
  <li><code>variable2</code> is ordered by </li>
  </ul>
  <p>How do I filter the rows such that <code>variable1</code> = <code>variable2</code>? Or can I modify the order of variable2 to match that of variable1?</p>
  <p>I created a small sample to replicate the dataset(to note, some full names contain 3 or more words):</p>
  <pre class="lang-r prettyprint-override"><code>library(tidyverse)
  
  name_surname &lt;- c(&quot;John Smith One&quot;, &quot;Jane Smith Two&quot;, &quot;John Doe&quot;, &quot;Nick Doe&quot;, &quot;Chris Froome&quot;, &quot;Van den Broeck&quot;, &quot;Lance&quot;, &quot;Van Dae Le Phillipe&quot;)
  
  surname_name &lt;- c(&quot;Smith One John&quot;, &quot;Smith Two Jane&quot;, &quot;Doe John&quot;, &quot;Nick Doe&quot;, &quot;Froome Chris&quot;, &quot;Broeck Van den&quot;, &quot;Lance&quot;, &quot;Phillipe Van Dae Le&quot;)
  
  tibble &lt;- tibble(variable1 = name_surname, variable2 = surname_name)
  
  tibble
  #&gt; # A tibble: 8 x 2
  #&gt;   variable1           variable2          
  #&gt;   &lt;chr&gt;               &lt;chr&gt;              
  #&gt; 1 John Smith One      Smith One John     
  #&gt; 2 Jane Smith Two      Smith Two Jane     
  #&gt; 3 John Doe            Doe John           
  #&gt; 4 Nick Doe            Nick Doe           
  #&gt; 5 Chris Froome        Froome Chris       
  #&gt; 6 Van den Broeck      Broeck Van den     
  #&gt; 7 Lance               Lance              
  #&gt; 8 Van Dae Le Phillipe Phillipe Van Dae Le
  </code></pre>
  <p><sup>Created on 2020-08-25 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>
  
  Session info
  <pre class="lang-r prettyprint-override"><code>devtools::session_info()
  #&gt; ─ Session info ───────────────────────────────────────────────────────────────
  #&gt;  setting  value                       
  #&gt;  version  R version 4.0.2 (2020-06-22)
  #&gt;  os       macOS Catalina 10.15.5      
  #&gt;  system   x86_64, darwin17.0          
  #&gt;  ui       X11                         
  #&gt;  language (EN)                        
  #&gt;  collate  en_AU.UTF-8                 
  #&gt;  ctype    en_AU.UTF-8                 
  #&gt;  tz       Australia/Melbourne         
  #&gt;  date     2020-08-25                  
  #&gt; 
  #&gt; ─ Packages ───────────────────────────────────────────────────────────────────
  #&gt;  package     * version date       lib source        
  #&gt;  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.2)
  #&gt;  backports     1.1.8   2020-06-17 [1] CRAN (R 4.0.2)
  #&gt;  blob          1.2.1   2020-01-20 [1] CRAN (R 4.0.2)
  #&gt;  broom         0.7.0   2020-07-09 [1] CRAN (R 4.0.2)
  #&gt;  callr         3.4.3   2020-03-28 [1] CRAN (R 4.0.2)
  #&gt;  cellranger    1.1.0   2016-07-27 [1] CRAN (R 4.0.2)
  #&gt;  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.2)
  #&gt;  colorspace    1.4-1   2019-03-18 [1] CRAN (R 4.0.2)
  #&gt;  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.2)
  #&gt;  DBI           1.1.0   2019-12-15 [1] CRAN (R 4.0.2)
  #&gt;  dbplyr        1.4.4   2020-05-27 [1] CRAN (R 4.0.2)
  #&gt;  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.2)
  #&gt;  devtools      2.3.1   2020-07-21 [1] CRAN (R 4.0.2)
  #&gt;  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.2)
  #&gt;  dplyr       * 1.0.1   2020-07-31 [1] CRAN (R 4.0.2)
  #&gt;  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.2)
  #&gt;  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.1)
  #&gt;  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.2)
  #&gt;  forcats     * 0.5.0   2020-03-01 [1] CRAN (R 4.0.2)
  #&gt;  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
  #&gt;  generics      0.0.2   2018-11-29 [1] CRAN (R 4.0.2)
  #&gt;  ggplot2     * 3.3.2   2020-06-19 [1] CRAN (R 4.0.2)
  #&gt;  glue          1.4.1   2020-05-13 [1] CRAN (R 4.0.2)
  #&gt;  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.2)
  #&gt;  haven         2.3.1   2020-06-01 [1] CRAN (R 4.0.2)
  #&gt;  highr         0.8     2019-03-20 [1] CRAN (R 4.0.2)
  #&gt;  hms           0.5.3   2020-01-08 [1] CRAN (R 4.0.2)
  #&gt;  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.2)
  #&gt;  httr          1.4.2   2020-07-20 [1] CRAN (R 4.0.2)
  #&gt;  jsonlite      1.7.0   2020-06-25 [1] CRAN (R 4.0.2)
  #&gt;  knitr         1.29    2020-06-23 [1] CRAN (R 4.0.2)
  #&gt;  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 4.0.2)
  #&gt;  lubridate     1.7.9   2020-06-08 [1] CRAN (R 4.0.2)
  #&gt;  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.2)
  #&gt;  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.2)
  #&gt;  modelr        0.1.8   2020-05-19 [1] CRAN (R 4.0.2)
  #&gt;  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.2)
  #&gt;  pillar        1.4.6   2020-07-10 [1] CRAN (R 4.0.2)
  #&gt;  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
  #&gt;  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.2)
  #&gt;  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.2)
  #&gt;  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.2)
  #&gt;  processx      3.4.3   2020-07-05 [1] CRAN (R 4.0.2)
  #&gt;  ps            1.3.3   2020-05-08 [1] CRAN (R 4.0.2)
  #&gt;  purrr       * 0.3.4   2020-04-17 [1] CRAN (R 4.0.2)
  #&gt;  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.2)
  #&gt;  Rcpp          1.0.5   2020-07-06 [1] CRAN (R 4.0.2)
  #&gt;  readr       * 1.3.1   2018-12-21 [1] CRAN (R 4.0.2)
  #&gt;  readxl        1.3.1   2019-03-13 [1] CRAN (R 4.0.2)
  #&gt;  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
  #&gt;  reprex        0.3.0   2019-05-16 [1] CRAN (R 4.0.2)
  #&gt;  rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
  #&gt;  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.2)
  #&gt;  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.2)
  #&gt;  rvest         0.3.6   2020-07-25 [1] CRAN (R 4.0.2)
  #&gt;  scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.2)
  #&gt;  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.2)
  #&gt;  stringi       1.4.6   2020-02-17 [1] CRAN (R 4.0.2)
  #&gt;  stringr     * 1.4.0   2019-02-10 [1] CRAN (R 4.0.2)
  #&gt;  testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.2)
  #&gt;  tibble      * 3.0.3   2020-07-10 [1] CRAN (R 4.0.2)
  #&gt;  tidyr       * 1.1.1   2020-07-31 [1] CRAN (R 4.0.2)
  #&gt;  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.2)
  #&gt;  tidyverse   * 1.3.0   2019-11-21 [1] CRAN (R 4.0.2)
  #&gt;  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.2)
  #&gt;  utf8          1.1.4   2018-05-24 [1] CRAN (R 4.0.2)
  #&gt;  vctrs         0.3.2   2020-07-15 [1] CRAN (R 4.0.2)
  #&gt;  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.2)
  #&gt;  xfun          0.16    2020-07-24 [1] CRAN (R 4.0.2)
  #&gt;  xml2          1.3.2   2020-04-23 [1] CRAN (R 4.0.2)
  #&gt;  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.2)
  #&gt; 
  #&gt; [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
  </code></pre>