Reshaping data in R with “login” “logout” times

后端 未结 6 1095
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-08 00:31

I\'m new to R, and am working on a side project for my own purposes. I have this data (reproducable dput of this is at the end of the question):

     X              


        
6条回答
  •  时光说笑
    2021-01-08 01:16

    Base version:

    samp$count <- with(samp, ave(as.character(user),list(state,user),FUN=seq_along) )
    
    out <- merge(
      samp[samp$state=="joined",c("user","datetime","count")],
      samp[samp$state=="left",c("user","datetime","count")],
      by=c("user","count"), all.x=TRUE
    )
    
    out[order(out$count),]
    

提交回复
热议问题