Counting number of rows between rows

后端 未结 2 1329
無奈伤痛
無奈伤痛 2021-01-24 20:55

I have a data frame with four columns: user_id, event, and time

User_id a user_id, event is either \"A\" or \"B\", and time is time. I need to count the number of \"B\"

2条回答
  •  余生分开走
    2021-01-24 21:30

    Using @r2Evans' data :

    x$y    <- NA
    which_ <- which(x$event=="A")
    x$y[which_] <- diff(c(0,which_))-1
    
    #    user_id event  date_time desired_column  y
    # 1        1     B 2018-01-01             NA NA
    # 2        1     B 2018-01-02             NA NA
    # 3        1     B 2018-01-03             NA NA
    # 4        1     B 2018-01-04             NA NA
    # 5        1     B 2018-01-05             NA NA
    # 6        1     A 2018-01-06              5  5
    # 7        1     B 2018-01-07             NA NA
    # 8        1     A 2018-01-08              1  1
    # 9        2     B 2018-01-05             NA NA
    # 10       2     B 2018-01-06             NA NA
    # 11       2     A 2018-01-07              2  2
    

提交回复
热议问题