2017-04-05 1 views
0

data.frame에는 개별 근로자에 ​​대한 로그와 병원의 특정 구내에서 시간을 보내는 곳이 포함되어 있습니다. 지금은 모든 노동자를 따라, 각 구청에 대한 고유 ID를 포함하는 열을 추가 할중복을 허용하는 두 개의 열을 따라 ID를 만드는 방법은 무엇입니까?

 Shift Worker   Ward Duration 
    <fctr> <fctr>   <fctr> <dbl> 
1  R1 Daniel   General 10 
2  R1 Daniel   General 15 
3  R2 Daniel   Anaesth 11 
4  R2 Daniel   Anaesth 13 
5  R2 Daniel   Anaesth 4 
6  R2 Daniel   General 15 
7  R2 Daniel   General 35 
8  R2 Daniel   Anaesth 6 
9  R2 Daniel   Anaesth 6 
10  R1 Caleb   Plastics 10 
11  R1 Caleb   Plastics 9 
12  R1 Caleb   Plastics 10 
13  R1 Caleb   Neuro  9 
14  R1 Caleb   Neuro  9 
15  R1 Caleb   Plastics 10 
16  R1 Caleb   Plastics 10 

나는 ID가 누적 될 및 중복 허용 할 있지만, 다음과 같이 data.frame가 구성되어있다. 내 예상 출력은 다음과 같습니다.

 Shift Worker   Ward Duration  ID 
    <fctr> <fctr>   <fctr> <dbl>  <fctr> 
1  R1 Daniel   General 10   1 
2  R1 Daniel   General 15   1 
3  R2 Daniel   Anaesth 11   2 
4  R2 Daniel   Anaesth 13   2 
5  R2 Daniel   Anaesth 4   2 
6  R2 Daniel   General 15   3 
7  R2 Daniel   General 35   3 
8  R2 Daniel   Anaesth 6   4 
9  R2 Daniel   Anaesth 6   4 
10  R1 Caleb   Plastics 10   1 
11  R1 Caleb   Plastics 9   1 
12  R1 Caleb   Plastics 10   1 
13  R1 Caleb   Neuro  9   2 
14  R1 Caleb   Neuro  9   2 
15  R1 Caleb   Plastics 10   3 
16  R1 Caleb   Plastics 10   3 

ID가 누적되는 방식에 유의하십시오. 어떻게해야합니까?

내가이 신분증을 원한 이유는 각 교대와 노동자에 따라 와드의 첫 번째와 마지막 신구를 부르는 것입니다. 내 예상 출력은 다음과 같습니다 :

 Shift Worker   Ward Duration  ID 
    <fctr> <fctr>   <fctr> <dbl>  <fctr> 
1  R1 Daniel   General 10   1 
2  R1 Daniel   General 15   1 
3  R2 Daniel   Anaesth 11   2 
5  R2 Daniel   Anaesth 4   2 
6  R2 Daniel   General 15   3 
7  R2 Daniel   General 35   3 
8  R2 Daniel   Anaesth 6   4 
9  R2 Daniel   Anaesth 6   4 
10  R1 Caleb   Plastics 10   1 
12  R1 Caleb   Plastics 10   1 
13  R1 Caleb   Neuro  9   2 
14  R1 Caleb   Neuro  9   2 
15  R1 Caleb   Plastics 10   3 
16  R1 Caleb   Plastics 10   3 

이렇게 할 방법이 있습니까? 도움을 주시면 대단히 감사하겠습니다.

+0

'라이브러리 (dplyr); df %> % group_by (Worker) %> % mutate (ID = data.table :: rleid (구))'또는 전체 data.table에서'library (data.table); setDT (df) [, ID : = rleid (구), by = Worker] []' – alistaire

답변

2

우리는 cumsum로 다음과 성과를 제거하는 '워드'즉, 필요한 출력을 얻을에 인접한 요소를 비교하여 '노동자'로 그룹화 한 후이 작업을 수행 할 수 있습니다

library(dplyr) 
df1 %>% 
    group_by(Worker) %>% 
    mutate(ID = cumsum(c(TRUE, Ward[-1] != Ward[-n()]))) 
# Shift Worker  Ward Duration ID 
# <chr> <chr> <chr> <int> <int> 
#1  R1 Daniel General  10  1 
#2  R1 Daniel General  15  1 
#3  R2 Daniel Anaesth  11  2 
#4  R2 Daniel Anaesth  13  2 
#5  R2 Daniel Anaesth  4  2 
#6  R2 Daniel General  15  3 
#7  R2 Daniel General  35  3 
#8  R2 Daniel Anaesth  6  4 
#9  R2 Daniel Anaesth  6  4 
#10 R1 Caleb Plastics  10  1 
#11 R1 Caleb Plastics  9  1 
#12 R1 Caleb Plastics  10  1 
#13 R1 Caleb Neuro  9  2 
#14 R1 Caleb Neuro  9  2 
#15 R1 Caleb Plastics  10  3 
#16 R1 Caleb Plastics  10  3 

아니면 base R 옵션을 사용하여 인덱스를 ave으로하여 그룹을 얻을하는 것 rle

df1$ID <- with(df1, as.integer(ave(Ward, Worker, FUN = function(x) 
         with(rle(x), rep(seq_along(values), lengths))))) 
관련 문제