rdataframederivedpartial-matches

Creating new column based on values in preceding column


I would like to add a new column to a data.frame that converts from the numeric value in the first column to the corresponding string (if any) from a subsequent matching column i.e. the column name partially matches this value in the first column.

In this example, I wish to add a value for 'Highest_Earner', which depends on the value in the Earner_Number column:

> df1 <- data.frame("Earner_Number" = c(1, 2, 1, 5),
                    "Earner5" = c("Max", "Alex", "Ben", "Mark"),
                    "Earner1" = c("John", "Dora", "Micelle", "Josh"))
> df1
  Earner_Number Earner5 Earner1
1             1     Max    John
2             2    Alex    Dora
3             1     Ben Micelle
4             5    Mark    Josh

The result should be:

> df1
  Earner_Number Earner5 Earner1 Highest_Earner
1             1     Max    John           John
2             2    Alex    Dora        Neither
3             1     Ben Micelle       Michelle
4             5    Mark    Josh           Mark

I have tried cutting the data.frame into various smaller pieces, but was wondering if someone had a somewhat cleaner method?


Solution

  •     #Have to convert them to character for nested if else to work.
    
        df$Earner5 <- as.character(df$Earner5)
        df$Earner1 <- as.character(df$Earner1)
    
        #Using nested if to get your column.
        df$Higher_Earner <-    ifelse(df$Earner_Number == 5, df$Earner5, 
                                          ifelse(df$Earner_Number==1df$Earner1,"Neither"))