title: Combine ifelse two conditions and loop
body: <p>I have a liste of dataframes (file1, file2, ..., file 72). For each dataframe I want to create one variable containing information from another dataframe based on two conditions.</p>
  <p>The idea is simple:</p>
  <ul>
  <li>condition 1: if file*$countryid equals source$country, and</li>
  <li>condition 2: if file*$year is higher than source$starting but lower than source$ending, then if true I want to create a column file*$rank with the value in source$rank</li>
  </ul>
  <p>I have been trying code lines like this but this code does not go through all lines in source:</p>
  <pre><code>file1$rank&lt;-ifelse(file1$countryid=source$countryid &amp; file1$year&gt;source$starting &amp; file1$year&lt;source$ending,source$rank,NA)
  </code></pre>
  <p>In addition I would like to implement this within a loop to avoid iterating manually through all these dataframes:</p>
  <pre><code>dflist&lt;-Filter(is.data.frame, mget(ls()))
  dflist&lt;-function(df,x){df$rank&lt;-ifelse(df$countryid=source$countryid &amp; df$year&gt;source$starting &amp; df$year&lt;source$ending,source$rank,NA))
  
  </code></pre>
  <p>Here is an example of the data I have.
  Thank you!</p>
  <pre><code>&gt; dput(file1)
  structure(list(id = c(1, 2, 3), countryid = c(10, 10, 13), year = c(1948, 
  1954, 1908)), row.names = c(NA, -3L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, 
  &quot;data.frame&quot;))
  </code></pre>
  <pre><code>dput(file2)
  structure(list(id = c(1, 2, 3), countryid = c(13, 10, 13), year = c(1907, 
  1908, 1907)), row.names = c(NA, -3L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, 
  &quot;data.frame&quot;))
  </code></pre>
  <pre><code>&gt; dput(source)
  structure(list(country = c(13, 13, 13, 10, 10, 10), rank = c(1, 
  2, 3, 1, 2, 3), starting = c(1885, 1909, 1940, 1902, 1907, 1931
  ), ending = c(1908, 1939, 1960, 1906, 1930, 1960)), row.names = c(NA, 
  -6L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>