0

私は3つのテーブルを持っています

最初のテーブルには、ID First_date、Birthday、Sex が含まれます

ID First_date Birthday Sex
1    19970104 19940921   M
2    19970107 19561224   F
3    20061228 19320426   M
4    20070231 19780825   F
5    20071231 19421206   F

2 番目のテーブルには ID event_type event_time が含まれます

ID  event_type  event_time
2       1        19990211     
4       1        20071226

3 番目のテーブルには ID event_type event_time が含まれます

ID  event_type   event_time
1     2          19990219
3     2          20070228

そして、テーブルを統合したい

ID   First_date Birthday Sex  event_type   event_time
1    19970104 19940921   M      2          19990219
2    19970107 19561224   F      1          19990211 
3    20061228 19320426   M      2          20070228
4    20070231 19780825   F      1          20071226
5    20071231 19421206   F      0             NA    

id5 は 2 と 3 のテーブルになかったので、event_type は 0 をコーディングします

4

1 に答える 1

0

ここで使用できmergeます:

 res <- merge(dat1,rbind(dat2,dat3),all.x=TRUE)
 ID First_date Birthday Sex event_type event_time
1  1   19970104 19940921   M          2   19990219
2  2   19970107 19561224   F          1   19990211
3  3   20061228 19320426   M          2   20070228
4  4   20070231 19780825   F          1   20071226
5  5   20071231 19421206   F         NA         NA

次に、逃した event_type に 0 を割り当てることができます。

res$event_type[is.na(res$event_type)] <- 0

ここでデータテーブルは次のとおりです。

dat1 <- read.table(text='ID First_date Birthday Sex
1    19970104 19940921   M
2    19970107 19561224   F
3    20061228 19320426   M
4    20070231 19780825   F
5    20071231 19421206   F',header=TRUE)

dat2 <- read.table(text='ID  event_type  event_time
2       1        19990211     
4       1        20071226',header=TRUE)

dat3 <- read.table(text='ID  event_type   event_time
1     2          19990219
3     2          20070228',header=TRUE)
于 2013-05-25T12:21:03.460 に答える