Calculate elapsed “times”, where the reference time depends on a factor

后端 未结 2 1655
栀梦
栀梦 2021-01-26 14:27

I\'m trying to calculate elapsed times in a data frame, where the \'start\' value for the elapsed time depends on the value of a factor column in the data frame. (To simply the

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-26 15:12

    Here is a ddply solution

    ddply(df, .(id), summarize, time = time, elapsed = seq(length(id))-1)
    

    and one using rle instead

    df$elapsed <- unlist(sapply(rle(as.numeric(df$id))$lengths, seq))-1
    

提交回复
热议问题