dimanche 28 août 2016

if statements on rows r

I have currently have a data frame that is taken from a data feed of events that happened in chronological order. I would like to add a new column onto to each row of my data the corresponds to the next event's player_id if the event type is 1 & the outcome is 1

e.g

player_id <- c(12, 17, 26, 3)
event_type <- c(1, 3, 1, 10)
outcome <- c(1, 0, 1, 1)
df <- data.frame(player_id, event_type, outcome)
df
     player_id  event_type outcome
1        12          1       1
2        17          3       0
3        26          1       1
4         3         10       1

so end result

      player_id event_type outcome next_player_id
1        12          1       1             17
2        17          3       0             NA
3        26          1       1              3
4         3         10       1             NA

Help much appreciated as I'm still in the beginners stage in r

Aucun commentaire:

Enregistrer un commentaire