2017-04-05 20 views
0

私のdata.frameには、個々の労働者のログと、病院の特定の病棟で時間を過ごす場所が含まれています。私は今、すべての労働者に沿って、各病棟の一意のIDを含む列を追加したい2つの列に沿って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がどのように蓄積されるのでしょうか。どうすればいいですか?

私がこの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(Ward)) 'または完全なdata.table、' library(data.table); setDT(df)[、ID:= rleid(区)、by =ワーカー] [] ' – 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))))) 
関連する問題