I'm new to R and I'm trying to make a list of matrix by filtering from a big matrix. So I have 2 matrix x and y
x1<-c(10,21,10)
y1<-rep(x1,2)
x2<-c(1,10,1)
y2<-c(x2-1,x2+1)
x3<-runif(3,0,1)
y3<-runif(6,0,1)
x<-as.matrix(cbind(x1,x2,x3))
y<-as.matrix(cbind(y1,y2,y3))
> x
x1 x2 x3
[1,] 10 1 0.6194646
[2,] 21 10 0.2522130
[3,] 10 1 0.9475642
> y
y1 y2 y3
[1,] 10 0 0.1853506
[2,] 21 9 0.5996231
[3,] 10 0 0.9127358
[4,] 10 2 0.1425093
[5,] 21 11 0.8909606
[6,] 10 2 0.8908601
I want to make a list of matrix, where the second row of each matrix is each row from matrix x, the first and third row of each matrix will be filtered from matrix y with conditions: y1==x1 and y2==x2-1 (1st row) and y2==x2+1 (3rd row). If the (x2-1) or (x2+1) have more than 1 row, I'll randomly pick 1 row. The result should be like this
> mt
[[1]]
x1 x2 x3
[1,] 10 0 0.9127358
[2,] 10 1 0.6194646
[3,] 10 2 0.8908601
[[2]]
x1 x2 x3
[1,] 21 9 0.5996231
[2,] 21 10 0.2522130
[3,] 21 11 0.8909606
[[3]]
x1 x2 x3
[1,] 10 0 0.1853506
[2,] 10 1 0.9475642
[3,] 10 2 0.1425093
I have written this code
mt<-list()
for (i in 1:nrow(x)){
b<-matrix((y[(y[,1]==x[i,1] & y[,2]==(x[i,2]-1)),]),ncol=ncol(y))
a<-matrix((y[(y[,1]==x[i,1] & y[,2]==(x[i,2]+1)),]),ncol=ncol(y))
mt[[i]]<-rbind(b[sample(nrow(b),size=1),],x[i,],a[sample(nrow(a),size=1),])
}
and it works fine. However, since I have 1M rows for matrix x, R will take very long to run. I'm looking for a more efficient way, can use Rcpp if possible since c++ is much faster than R. Thank you very much!
question from:
https://stackoverflow.com/questions/65851477/efficient-way-to-filter-and-make-a-list-of-matrix-in-r