在R中设置对矩阵的运算

时间:2016-02-21 01:15:20

标签: r matrix set union intersection

我目前在R中编写代码,主要依赖于在矩阵上使用set操作。我想知道是否有任何包或预先存在的函数可以比较矩阵的一行中的每个值。例如,如果我有以下两个矩阵:

A1                     A2
1   1 -1.579122144     10  0.577115944
2   2 -1.620980244     11  1.263320594
3   3 -1.156350422     12 -0.524403635
4   4  0.948909066     13 -1.363554588
5   5 -0.719925025     14  1.698710773
6   6  0.642472705     15  0.957670227
7   7  0.611488905     16 -1.579122144
8   8 -1.048742921     17 -1.620980244
9   9 -0.006137041     18 -1.156350422
10 10  0.577115944     19  0.948909066
11 11  1.263320594     20 -0.719925025
12 12 -0.524403635     21  0.642472705
13 13 -1.363554588     22  0.611488905
14 14  1.698710773     23 -1.048742921
15 15  0.957670227     24 -0.006137041

假设的交叉函数将返回如下内容:

>intersect(A1,A2)
10  0.5771159
11  1.2633206
12 -0.5244036
13 -1.3635546
14  1.6987108
15  0.9576702

也许subset函数可以在这里工作,但我不确定如何实现它。任何有关设置差异,交叉点和联合的功能的帮助都将非常感激。

2 个答案:

答案 0 :(得分:2)

已修改为添加2个套餐:sqldfdplyr

您需要的输出可以通过sqldf包来实现。将矩阵转换为数据帧。因此:

A1<-data.frame(x=c(-1.579122144,-1.62098024,-1.156350422,0.948909066,-0.719925025,0.642472705,0.611488905,-1.048742921,-0.006137041,0.577115944,1.263320594,-0.524403635,-1.363554588,1.698710773,0.957670227), id=1:15)
A2<-data.frame(x=c(0.577115944,1.263320594,-0.524403635,-1.363554588,1.698710773,0.957670227,-1.579122144,-1.62098024,-1.156350422,0.948909066,-0.719925025,0.642472705,0.611488905,-1.048742921,-0.006137041), id=10:24)

现在使用sqldf包:

library (sqldf)

sqldf("select * from A1 intersect select * from A2")

将为您提供输出:

x id
1 -1.3635546 13
2 -0.5244036 12
3  0.5771159 10
4  0.9576702 15
5  1.2633206 11
6  1.6987108 14


您可以使用dplyr包并使用intersect功能:

library(dplyr)

intersect(A1,A2, by="x")

这两个软件包都应具有union setdiff等功能

答案 1 :(得分:0)

基础R merge()执行此操作,唯一需要注意的是它强制使用矩阵到data.frames,这就是列名V1V2出现在以下输出中的原因:< / p>

A1 <- matrix(c(1:15,-1.579122144,-1.62098024,-1.156350422,0.948909066,-0.719925025,0.642472705,0.611488905,-1.048742921,-0.006137041,0.577115944,1.263320594,-0.524403635,-1.363554588,1.698710773,0.957670227),15L);
A2 <- matrix(c(10:24,0.577115944,1.263320594,-0.524403635,-1.363554588,1.698710773,0.957670227,-1.579122144,-1.62098024,-1.156350422,0.948909066,-0.719925025,0.642472705,0.611488905,-1.048742921,-0.006137041),15L);
o <- lapply(list(A1,A2),capture.output); cat(paste0('A1',paste0(collapse='',rep(' ',nchar(o[[1]][1])-2L+3L)),'A2'),do.call(paste,c(o,sep='   ')),sep='\n'); ## unnecessarily complex line to print the two matrices side-by-side
## A1                        A2
##       [,1]         [,2]         [,1]         [,2]
##  [1,]    1 -1.579122144    [1,]   10  0.577115944
##  [2,]    2 -1.620980240    [2,]   11  1.263320594
##  [3,]    3 -1.156350422    [3,]   12 -0.524403635
##  [4,]    4  0.948909066    [4,]   13 -1.363554588
##  [5,]    5 -0.719925025    [5,]   14  1.698710773
##  [6,]    6  0.642472705    [6,]   15  0.957670227
##  [7,]    7  0.611488905    [7,]   16 -1.579122144
##  [8,]    8 -1.048742921    [8,]   17 -1.620980240
##  [9,]    9 -0.006137041    [9,]   18 -1.156350422
## [10,]   10  0.577115944   [10,]   19  0.948909066
## [11,]   11  1.263320594   [11,]   20 -0.719925025
## [12,]   12 -0.524403635   [12,]   21  0.642472705
## [13,]   13 -1.363554588   [13,]   22  0.611488905
## [14,]   14  1.698710773   [14,]   23 -1.048742921
## [15,]   15  0.957670227   [15,]   24 -0.006137041
merge(A1,A2);
##   V1         V2
## 1 10  0.5771159
## 2 11  1.2633206
## 3 12 -0.5244036
## 4 13 -1.3635546
## 5 14  1.6987108
## 6 15  0.9576702

您可以使用as.matrix()恢复矩阵,如果需要,可以使用unname()删除列名。