R: column binding with unequal number of rows -



R: column binding with unequal number of rows -

i have 2 info sets. each of them has variables id, block, , rt (reaction time). want merge/column bind 2 sets have 1 info set variables: id, block, rt1, rt2. problem there unequal number of rows in 2 sets. also, of import id , block number match. missing values should replaced na. have:

head(blok1, 10) id blok rt1 1 1 1 592 2 1 1 468 3 1 1 530 4 1 1 546 5 1 1 452 6 1 1 483 7 1 2 499 8 1 2 452 9 1 2 608 10 1 2 530 head(blok2, 10) id blok rt2 1 1 1 592 2 1 1 920 3 1 1 686 4 1 1 561 5 1 1 561 6 1 2 327 7 1 2 686 8 1 2 670 9 1 2 702 10 1 3 920

what want have:

id blok rt1 rt2 1 1 1 592 592 2 1 1 468 920 3 1 1 530 686 4 1 1 546 561 5 1 1 452 561 6 1 1 483 na 7 1 2 499 327 8 1 2 452 686 9 1 2 608 670 10 1 2 530 702

etc.

here's solution using dplyr, utilizing index or unique id:

blok1 <- data.frame(id = c(1, 1, 2), rt1 = c(11, 12, 13)) blok2 <- data.frame(id = c(1, 2, 2), rt2 = c(21, 22, 23)) library(dplyr) ## if want nas rt2 blok1 %>% mutate(uid = row_number()) %>% left_join(blok2 %>% mutate(uid = row_number()), = c("uid", "id")) # uid id rt1 rt2 # 1 1 1 11 21 # 2 2 1 12 na # 3 3 2 13 23 ## if want nas both rt1 , rt2 blok1 %>% mutate(uid = row_number()) %>% outer_join(blok2 %>% mutate(uid = row_number()), = c("uid", "id")) # uid id rt1 rt2 # 1 1 1 11 21 # 2 2 1 12 na # 3 3 2 13 23 # 4 2 2 na 22

r binding merge

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -