Search code examples
rsubsetdummy-variable

How do you apply if else statements across user id #?


I am trying to create a dummy variable that flags the user id of people who attended a specific event. Each user id has multiple rows and I would like this dummy variable to apply to every row of the flagged user id. For example, using the data set below, I would like to flag the user IDs of everyone who attended "event b" (using a "1" for attended event b and "0" for did not attend event b). The tricky part is that I want the 1 to appear in every row that matches the user IDs of the people who attended "event b".

I want to use this dummy variable to eventually subset the data so that I can assess the event attending patterns of the users who attended a particular event.

df<-data.frame(id=(100,100,100,101,101,102,102,103,103,103,103),
             event=("a","b","c","b","d","a","c","a","c","d","e"))

Solution

  • Consider ifelse and ave, iterating across unique values or levels of event

    for(ev in unique(df$event)) {   # for(ev in levels(df$event)) {
        df[[paste0("event_", ev, "_flag")]] <- with(df, ave(ifelse(event == ev, 1, 0), id, FUN=max))
    }
    
    df
    #     id event event_a_flag event_b_flag event_c_flag event_d_flag event_e_flag
    # 1  100     a            1            1            1            0            0
    # 2  100     b            1            1            1            0            0
    # 3  100     c            1            1            1            0            0
    # 4  101     b            0            1            0            1            0
    # 5  101     d            0            1            0            1            0
    # 6  102     a            1            0            1            0            0
    # 7  102     c            1            0            1            0            0
    # 8  103     a            1            0            1            1            1
    # 9  103     c            1            0            1            1            1
    # 10 103     d            1            0            1            1            1
    # 11 103     e            1            0            1            1            1