Search code examples
rdata.tablesubsetrecode

Adding suffix to values meeting a condition(s) in R


I am trying to add a suffix letter based on a character value of another variable. Whenever I see an "e" in the category variable, then the id should have three rows like i_C, i_E, and i_O. This means that this id has three dimensions of C, E, and O. That is why I want to have three rows of that specific i.

Here is a sample data:

id <- c("i1","i2","i3","i4","i5")
category <- c("a", "b", "c", "d", "e")

data <- data.frame("id"=id, "category"=category)
  id category
1 i1        a
2 i2        b
3 i3        c
4 i4        d
5 i5        e

I ran this code but it could only produce i1_A.

library(data.table)
setDT(data)
data[category == "e", id := paste0(id, "_", "A")]

> data
     id category
1:   i1        a
2:   i2        b
3:   i3        c
4:   i4        d
5: i5_A        e

What I want to have is:

> data
     id category
1:   i1        a
2:   i2        b
3:   i3        c
4:   i4        d
5: i5_A        e
6: i5_B        e
7: i5_C        e

Any suggestions?


Solution

  • I'm not entirely sure, but here is what I think you are after:

    library(data.table)
    
    DT <- data.table(id = paste0("i", seq_len(5)), category = letters[seq_len(5)])
    suffixTemplate <- data.table(suffix = paste0("_", LETTERS[seq_len(3)]), category = "e")
    
    suffixDT <- DT[category == "e"][suffixTemplate, on = "category"]
    suffixDT[, id := paste0(id, suffix)]
    suffixDT[, suffix := NULL]
    
    resultDT <- rbindlist(list(suffixDT, DT[category != "e"]), use.names = TRUE)
    
    setorder(resultDT, id)
    print(resultDT)
    

    Result:

         id category
    1:   i1        a
    2:   i2        b
    3:   i3        c
    4:   i4        d
    5: i5_A        e
    6: i5_B        e
    7: i5_C        e