Vectorized solution to assignment to specific rows by columns in R
I have three matrices:
a<-sapply(1:300, function(x) sample(1:100,100*0.8,replace=F))
b<-sapply(1:300, function(x) sample(1:2,100*0.8, replace=TRUE))
c<-matrix(data=NA, nrow=100, ncol=300)
I would like to assign to do the following assignment:
select those rows in c[,1] that are in a[,1] and assign b[,1] to them.
do this for all c[,1:300], a[,1:300], b[,1:300]
For one column I would do:
c[a[,1],1]<-b[,1]
but I would like to do this for all columns.
Is there a vectorized solution to this?
No comments:
Post a Comment