R:行数不相等的列绑定 [英] R: column binding with unequal number of rows

查看:127
本文介绍了R:行数不相等的列绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据集.它们每个都有变量ID,Block和RT(反应时间).我想合并/列绑定这两个集合,以便我有一个带有变量的数据集:ID,Block,RT1,RT2.问题是两组中的行数不相等.同样,ID和块编号匹配也很重要.缺少的值应替换为NA.所以我有:

I have two data sets. Each of them has variables ID, Block, and RT (reaction time). I want to merge/column bind the two sets so that I have one data set with variables: ID, Block, RT1, RT2. The problem is there is unequal number of rows in the two sets. Also, it is important that the ID and block number match. Missing values should be replaced with NA. So what I 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

我想要拥有的东西:

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 

推荐答案

以下是使用dplyr的解决方案,也使用了索引或唯一ID:

Here's a solution using dplyr, also utilizing an 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 you want NAs for RT2 only
blok1 %>%
  mutate(uID = row_number()) %>%
  left_join(blok2 %>% mutate(uID = row_number()), by = c("uID", "ID"))
# uID ID RT1 RT2
# 1   1  1  11  21
# 2   2  1  12  NA
# 3   3  2  13  23

## if you want NAs for both RT1 and RT2
blok1 %>%
  mutate(uID = row_number()) %>%
  outer_join(blok2 %>% mutate(uID = row_number()), by = 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:行数不相等的列绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆