[英]Matching, merging, finding duplicated value in R
In R, I want to match and merge two matrices. 在R中,我想匹配和合并两个矩阵。
For example, 例如,
> A
ID a b c d e f g
1 ex 3 8 7 6 9 8 4
2 am 7 5 3 0 1 8 3
3 ple 8 5 7 9 2 3 1
> B
col1
1 a
2 c
3 e
4 f
Then, I want to match header of matrix A and 1st column of matrix B. 然后,我要匹配矩阵A的标题和矩阵B的第一列。
So I did 所以我做了
> C<-A[, c('ID', B[, 1])]
and the final result was like below. 最终结果如下所示。
> C
ID a c e f
1 ex 3 7 9 8
2 am 7 3 1 8
3 ple 8 7 2 3
However, if the matrix B has some values that are not in the matrix A like below, 但是,如果矩阵B的某些值不在矩阵A中,如下所示,
> B
col1
1 a
2 c
3 e
4 f
5 x
6 y
It says 'subscript out of bounds'. 它说“下标超出范围”。
How can I avoid this problem? 如何避免这个问题?
(How to extract duplicated columns only?) (如何仅提取重复的列?)
查看是否可行:
C<-A[, c('ID', colnames(A)[colnames(A) %in% B[,1]])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.