Search code examples
rdataframedatatablerecode

How to assign a value to a column based on value in other column in R data frame or data table


I hope you can help me here. I found a lot of basic recode answers but none that I could adapt to my problem. The problem is: For all rows, I want to set the column Level_eng_1 to the value of colum verb_e for each row where the column parent_concept of this row matches the ID column.

I tried to adapt numerous solutions existing on stackoverflow but did not succeed.

Here is the data.

structure(list(ID = c(1, 2, 3, 11, 12, 13, 14, 16, 20, 21, 22, 
23, 24, 25, 30, 31, 32, 33, 34), Parent_Concept = c(0L, 0L, 0L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L
), verb_e = c("act", "change", "move", "support", "regulate", 
"interact", "structure", "communicate", "time", "make", "decrease", 
"increase", "modify", "orientate", "motion", "inhibit", "bring_together", 
"separate", "transmit"), Level_eng_1 = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 19L), class = "data.frame")

The desired output is:

structure(list(ID = c(1, 2, 3, 11, 12, 13, 14, 16, 20, 21, 22, 
23, 24, 25, 30, 31, 32, 33, 34), Parent_Concept = c(0L, 0L, 0L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L
), verb_e = c("act", "change", "move", "support", "regulate", 
"interact", "structure", "communicate", "time", "make", "decrease", 
"increase", "modify", "orientate", "motion", "inhibit", "bring_together", 
"separate", "transmit"), Level_eng_1 = c("act", "change", "move", 
"act", "act", "act", "act", "act", "change", "change", "change", 
"change", "change", "change", "move", "move", "move", "move", 
"move")), row.names = c(NA, 19L), class = "data.frame")

Thanks for helping!


Solution

  • May be this would help

    library(dplyr)
    input %>% 
        mutate(Level_eng_1 = verb_e[match(Parent_Concept, ID)], 
               Level_eng_1 = case_when(is.na(Level_eng_1) ~ verb_e, TRUE  ~Level_eng_1))
    #    ID Parent_Concept         verb_e Level_eng_1
    #1   1              0            act         act
    #2   2              0         change      change
    #3   3              0           move        move
    #4  11              1        support         act
    #5  12              1       regulate         act
    #6  13              1       interact         act
    #7  14              1      structure         act
    #8  16              1    communicate         act
    #9  20              2           time      change
    #10 21              2           make      change
    #11 22              2       decrease      change
    #12 23              2       increase      change
    #13 24              2         modify      change
    #14 25              2      orientate      change
    #15 30              3         motion        move
    #16 31              3        inhibit        move
    #17 32              3 bring_together        move
    #18 33              3       separate        move
    #19 34              3       transmit        move
    

    data

    input <-  structure(list(ID = c(1, 2, 3, 11, 12, 13, 14, 16, 20, 21, 22, 
    23, 24, 25, 30, 31, 32, 33, 34), Parent_Concept = c(0L, 0L, 0L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L
    ), verb_e = c("act", "change", "move", "support", "regulate", 
    "interact", "structure", "communicate", "time", "make", "decrease", 
    "increase", "modify", "orientate", "motion", "inhibit", "bring_together", 
    "separate", "transmit"), Level_eng_1 = c(NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 
    19L), class = "data.frame")