2016-08-01 2 views
-2

내가 (입력)과 같은 테이블이 : 테이블의이벤트 전 마지막 로그를 아는 방법은 무엇입니까? R 언어

user_id event  timestamp 
Rob  business 111111 
Rob  business 222222 
Mike  progress 111111 
Mike  progress 222222 
Rob  progress 000001 
Mike  business 333333 
Mike  progress 444444 
Lee  progress 111111 
Lee  progress 222222 

dput :

: 내가 처음 business 이벤트 (출력)이 발생하기 전에 마지막 progress 이벤트를 알고 싶은

dput(input) 
structure(list(user_id = structure(c(3L, 3L, 2L, 2L, 3L, 2L, 
2L, 1L, 1L), .Label = c("Lee", "Mike", "Rob"), class = "factor"), 
    event = structure(c(1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L), .Label = c("business", 
    "progress"), class = "factor"), timestamp = c(111111, 222222, 
    111111, 222222, 1, 333333, 444444, 111111, 222222)), .Names = c("user_id", 
"event", "timestamp"), row.names = c(NA, -9L), class = "data.frame") 

user_id event  timestamp 
    Mike  progress 222222 
    Rob  progress 000001 

도움 주셔서 감사합니다.

+3

더 잘 설명해야 할 것 같습니다. – Frank

답변

2

우리는 data.table

012으로 시도 할 수 있습니다
library(data.table) 
setDT(df1)[df1[order(as.numeric(timestamp)), if(any(event == "business")) 
     .I[tail(which(cumsum(event == "business")==0),1)], user_id]$V1] 
# user_id event timestamp 
#1:  Rob progress 000001 
#2: Mike progress 222222 
+1

좋은 근무 !!! 고마워 – Smasell

1

내가하려는 것을 완전히 얻었는지 확실하지 않습니다. which 사용하면 (데이터가 input라고합니다)가 아닌 모든 비즈니스 이벤트의 인덱스를 얻을 수 있습니다 :

indexes <- which(input$event != "business") 

는 그런 다음 마지막 비즈니스 이벤트까지 비 비즈니스 이벤트를 가질 인덱스의 벡터를 필터링 할 수 있습니다 :

indexes <- indexes[indexes < max(which(input$event == "business"))] 

나머지 행을 보면 우리는이 :

> input[indexes,] 
    user_id event timestamp 
3 Mike progress 111111 
4 Mike progress 222222 
5  Rob progress   1 
관련 문제