Search code examples
rdummy-variable

Edit row values for many dummy variables


This is how my data looks:

Input

My dput is given below as:

structure(list(id = c(1, 1, 3, 3, 5, 6), country = c("US", "JP","CH", "US", "KR", "KR"), US = c(0, 0, 0, 0, 0, 0), JP = c(0,0, 0, 0, 0, 0), CH = c(0, 0, 0, 0, 0, 0), KR = c(0, 0, 0, 0,0, 0), BE = c(0, 0, 0, 0, 0, 0), SP = c(0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA, -6L))

This is what I want:

enter image description here

Running data2[levels(factor(data2$country))] = model.matrix(~country - 1, data2) gives me a close but WRONG result:

enter image description here

How can I get the structure that I'm looking for? Please help; I've been working on this problem for days and no advice I've gotten has been close to what I need.


Solution

  • One way you can do it using lapply and sapply

    #Get Matches for column name and country variable
    
    matches <- lapply(colnames(df[-(1:2)]), \(x) df$country %in% x)
    
    
    #Get the id for each match
    
    ids <- lapply(matches, \(x) df$id[x])
    
    #add 1 to each
    
    df[-(1:2)] <- sapply(ids, \(x) +(df$id %in% x))
    

    or in one go

    df[-(1:2)] <- sapply(lapply(lapply(colnames(df[-(1:2)]), 
                                       \(x) df$country %in% x),
                                \(x) df$id[x]), 
                         \(x) +(df$id %in% x))
    
      id country US JP CH KR BE SP
    1  1      US  1  1  0  0  0  0
    2  1      JP  1  1  0  0  0  0
    3  3      CH  1  0  1  0  0  0
    4  3      US  1  0  1  0  0  0
    5  5      KR  0  0  0  1  0  0
    6  6      KR  0  0  0  1  0  0