title: Is there a function for seeing how and how many times words from a variable appear in another variable with a dataset in R?
body: <p>I need help for find a code in R: I have a file excel composed from different variables, but i need to compare two variables (in my case are both name of people), for seeing if the names in the first variables(column) appear in the second variable (column) and how many times. Are very big dataset with a lot of row.
  Thank you in advance!!</p>
  <p>The Data is given as,</p>
  <pre><code>df &lt;- structure(
      list(
          Inv_name = c(
              &quot;Meyer, Helmut-Walter, Prof. Dr. sc.&quot;,
              &quot;Fröhlich, Hans-Otto, Prof. Dr.&quot;,
              &quot;Göbel, Andreas&quot;,
              &quot;Klein, Wilfried&quot;,
              &quot;Scholz, Peter, Dr.&quot;,
              &quot;Hipler, Bernd&quot;,
              &quot;Sammler, Marina&quot;,
              &quot;Kreisel, Günter, Dr.&quot;,
              &quot;Haupt, Kerstin&quot;,
              &quot;Bayer, Ulrich&quot;,
              &quot;Schmidt, Jürgen&quot;,
              &quot;Furche, Thomas&quot;,
              &quot;Thorwirth, Günter&quot;,
              &quot;Kowalski, Peter&quot;,
              &quot;Geiler, Hans-Dieter, Dr.&quot;,
              &quot;Wagner, Matthias, Dr.&quot;,
              &quot;RIESENBERG, Dieter&quot;,
              &quot;SCHULZ, Volker&quot;,
              &quot;KNORRE, Wofgang&quot;,
              &quot;POHL, Hans, Dieter&quot;
          ),
          Prio_Year = c(
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990,
              1990
          ),
          Firma = c(
              &quot;Gustav Peters&quot;,
              &quot;id - Informationsdienst, Geschäftsvermittlung, Handelsvertretung, Marketing Kursawe &amp; Klein OHG&quot;,
              &quot;\&quot;Heizhaus-Service\&quot; Ingenieurbüro Wilfried Gärtner&quot;,
              &quot;MAX LINDAU OHG Inhaber: Gerhard und Klaus-Dieter Lindau&quot;,
              &quot;Werther Götze \&quot;Erfordia Numismatica\&quot;&quot;,
              &quot;STUDIO 1 - John &amp; Ziemann&quot;,
              &quot;Arwico-Arenswald &amp; Wichmann OHG&quot;,
              &quot;Moschkowitz &amp; Co. Schweißtechnik-Vertriebs KG&quot;,
              &quot;Fahrschule Simon &amp; Frauendorf&quot;,
              &quot;Elmera Vertrieb Frank Fleischmann OHG&quot;,
              &quot;OHG Friseur Walschleben&quot;,
              &quot;Baumarkt Hartmut Seliger&quot;,
              &quot;\&quot;Thüringer MarketingService - Hoffmann &amp; Co. OHG\&quot;&quot;,
              &quot;Walter Reuning Fleisch-Wurstwaren Inhaber Brigitte Pfautsch&quot;,
              &quot;Thüringer Allgemeine Verlag GmbH &amp; Co, KG&quot;,
              &quot;Autoservice Grohmann-Lämmerhirt OHG&quot;,
              &quot;\&quot;Sippel und Thomas Video - Land\&quot; Offene Handelsgesellschaft&quot;,
              &quot;Paul Krause&quot;,
              &quot;Alois Reuter - Stapler-Instandsetzung&quot;,
              &quot;Rudolf Städtler Inhaber Willy Städtler&quot;
          ),
          Ersteintrag = structure(
              c(
                  -2209075200,
                  643420800,
                  643248000,
                  645235200,
                  646617600,
                  646617600,
                  646617600,
                  646963200,
                  648432000,
                  648432000,
                  649382400,
                  648777600,
                  648950400,
                  648950400,
                  649468800,
                  650073600,
                  650678400,
                  -2156112000,
                  650678400,
                  650678400
              ),
              tzone = &quot;UTC&quot;,
              class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;)
          )
      ),
      row.names = c(NA,-20L),
      class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;)
  )
  </code></pre>
  <p>The aim is to compare the the elements in <code>Inv_name</code> sequentially against all indices in <code>Firma</code>, and count number of partial matches such that if <code>df$Inv_name[[1]] = &quot;A&quot;</code> then the count would be <code>2</code>, if <code>df$Firma = c(&quot;A GMBH&quot;, &quot;B GMBH&quot;, &quot;A Inc&quot;)</code>.</p>
  <p>for (i in 1:nrow(df1)){</p>
  <ul>
  <li></li>
  <li>
  <pre><code># Extract the Inv_name
  </code></pre>
  </li>
  <li>
  <pre><code># as a matrix; this will be our pattern search
  </code></pre>
  </li>
  <li>
  <pre><code># along the Firma
  </code></pre>
  </li>
  <li>
  <pre><code>tmpName &lt;- str_to_lower(
  </code></pre>
  </li>
  <li>
  <pre><code>    str_split(df1$Inv_name[i],pattern = &quot;,&quot;,simplify = T)
  </code></pre>
  </li>
  <li>
  <pre><code>)
  </code></pre>
  </li>
  <li></li>
  <li>
  <pre><code># Create a new variable that counts the number of 
  </code></pre>
  </li>
  <li>
  <pre><code># occurences of the id
  </code></pre>
  </li>
  <li>
  <pre><code>    str_detect(
  </code></pre>
  </li>
  <li>
  <pre><code>        string  = str_to_lower(df1$Firma),
  </code></pre>
  </li>
  <li>
  <pre><code>        pattern = paste(tmpName, sep = &quot;&quot;, collapse = &quot;|&quot;)
  </code></pre>
  </li>
  <li>
  <pre><code>)
  </code></pre>
  </li>
  <li></li>
  <li>
  <pre><code># NOTE: That it it cannot count correctly
  </code></pre>
  </li>
  <li>
  <pre><code># due to titles like Dr. Mr. and MS.
  </code></pre>
  </li>
  <li>
  <pre><code># If you are certaing about the pattern on
  </code></pre>
  </li>
  <li>
  <pre><code># how these titles are generated then we can ignore these.
  </code></pre>
  </li>
  <li>
  <pre><code># Here we remove all matches on the form Dr. Mr. Ms.
  </code></pre>
  </li>
  <li>
  <pre><code>tmpName &lt;- str_remove_all(
  </code></pre>
  </li>
  <li>
  <pre><code>    tmpName,
  </code></pre>
  </li>
  <li>
  <pre><code>    pattern = &quot;[:alpha:]+[:punct:][:blank:]+&quot;
  </code></pre>
  </li>
  <li>
  <pre><code>)
  </code></pre>
  </li>
  <li></li>
  <li>
  <pre><code># We then recount without titles
  </code></pre>
  </li>
  <li>
  <pre><code># to avoid confusion
  </code></pre>
  </li>
  <li>
  <pre><code>df1$count2[i] &lt;- sum(
  </code></pre>
  </li>
  <li>
  <pre><code>    str_detect(
  </code></pre>
  </li>
  <li>
  <pre><code>        string  = str_to_lower(df1$Firma),
  </code></pre>
  </li>
  <li>
  <pre><code>        pattern = paste(tmpName, sep = &quot;&quot;, collapse = &quot;|&quot;)
  </code></pre>
  </li>
  <li>
  <pre><code>    )
  </code></pre>
  </li>
  <li>
  <pre><code>)
  </code></pre>
  </li>
  <li></li>
  <li>}
  Warning messages:
  1: Unknown or uninitialised column: <code>count1</code>.
  2: Unknown or uninitialised column: <code>count2</code>.</li>
  </ul>
  <blockquote>
  <p>tmpName
  [1] NA</p>
  </blockquote>