title: R: Transform dataframe column using dictionary/list?
body: <p>I have a dataframe column with numerous textual values (levels). I need to map those values to a predefined object-like structure in order to reduce the number of levels. I could easily achieve this in Python using a dictionary but could not do the same with a list in R.</p>
  
  <p>For example, my dataframe column is something like:</p>
  
  <pre><code>df &lt;- data.frame(weather = c('Clear','Snow','Clear','Rain','Rain','Other','Hail/sleet','Unknown'))
  </code></pre>
  
  <p>I need to map this to a list like</p>
  
  <pre><code>weather.levels &lt;- list(
    dry = c('Clear', 'Cloudy'),
    wet = c('Snow', 'Rain', 'Hail/sleet'),
    other = c('Other','Unknown'))
  </code></pre>
  
  <p>so that my transformed dataframe looks like</p>
  
  <pre><code>    old.weather new.weather
  1       Clear         dry
  2        Snow         wet
  3       Clear         dry
  4        Rain         wet
  5        Rain         wet
  6      Other1       other
  7  Hail/sleet         wet
  8     Unknown       other
  </code></pre>
  
  <p>I have looked at solutions like <a href="https://stackoverflow.com/questions/28751879/how-to-map-a-column-through-a-dictionary-in-r">this</a> and <a href="https://stackoverflow.com/questions/29454314/r-map-a-column-to-column-using-key-value-list?noredirect=1&amp;lq=1">this</a>, but these do not answer my question. I cannot create a dataframe to use R's <code>match</code> function because the number of levels in each category of the preset dictionary <code>weather.levels</code> ('dry', 'wet', 'other') are different.</p>