Search code examples
rdplyrmergecoalesce

Merge and replace character columns


I have a dataframe with 2 columns that I want to merge:

     Region             PA
1     Mbeya    Ruaha National Park
2     Mbeya    Ruaha National Park
3     Mbeya    Ruaha National Park
4     Mbeya    Ruaha National Park
5     Mbeya    Ruaha National Park
6     Mbeya    Ruaha National Park
7     Mbeya    NA
8     Mbeya    NA
9     Mbeya    NA
10    Mbeya    NA

This could be merged by either taking PA values and overwriting Region values in the rows, or replacing all NA's in PA with the values in Region for that row.

I've tried:

  Carcass.cleaned$New<-rowSums(Carcass.cleaned[, c("PA", "Region")], na.rm=T)
    Error in base::rowSums(x, na.rm = na.rm, dims = dims, ...) : 
      'x' must be numeric
    
    with(Carcass.cleaned,ifelse(is.na(PA),Region,PA))
    (returns list of numbers)
    
    and coalesce(Carcass.cleaned$PA, Carcass.cleaned$Region) 
    unite(Carcass.cleaned, new, PA:Region, sep='') 
 (both merge the columns names instead of replacing)

Solution

  • You can use a simple if else statement:

    df$Region <- ifelse(is.na(df$PA), df$Region, df$PA)
    

    Basically wherever PA is NA you leave Region untouched, and where PA has a value you overwrite the value in Region. Afterwords you can delete PA if you want