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.
For example, my dataframe column is something like:
df <- data.frame(weather = c('Clear','Snow','Clear','Rain','Rain','Other','Hail/sleet','Unknown'))
I need to map this to a list like
weather.levels <- list(
dry = c('Clear', 'Cloudy'),
wet = c('Snow', 'Rain', 'Hail/sleet'),
other = c('Other','Unknown'))
so that my transformed dataframe looks like
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
I have looked at solutions like this and this, but these do not answer my question. I cannot create a dataframe to use R's match
function because the number of levels in each category of the preset dictionary weather.levels
('dry', 'wet', 'other') are different.