Search code examples
rmerger-factor

Merging several factor variables


This is a follow-up to an earlier question I had: Combining Survey Items in R/ Recoding NAs

I have a data-frame that has multiple factor-variables that I want to combine into one variable.

ID      REGIONA REGIONB REGIONC
A        North     NA      NA
A        South     NA      NA
B        NA      East      NA
B        NA      West      NA
C        NA        NA     North
C        NA        NA     East

I want the combined data frame to look like this.

ID      REGION
A        North    
A        South     
B        East      
B        West      
C        North       
C        East     

Using the technique in the previous post within(df, x3 <- ifelse(is.na(x1), x2, x1))works for numbers, but does not seem to handle the factors well.


Solution

  • you need to use levels. for more info look at the help file for ?factor.

    within(df, x3 <- ifelse(is.na(x1), levels(x2)[x2], levels(x1)[x1]))
    

    Or with your example:

    within(df, x3 <- ifelse(!is.na(REGIONA), 
                            levels(REGIONA)[REGIONA], 
                            ifelse(!is.na(REGIONB), 
                                   levels(REGIONB)[REGIONB],
                                   levels(REGIONC)[REGIONC])))