matrix - R Two matrices, extract rows from m based on common column of r -
matrix - R Two matrices, extract rows from m based on common column of r -
my question matrixes r. have 2 matrices r , m:
m <- as.matrix(read.table(text=" 15 56 44 1 4 7 61 31 63 7 1 3 10 36 99 5 9 6 65 79 88 54 1 1")) colnames(m) <- c("z","q","a","f","d","h") r <- as.matrix(read.table(text=" 15 56 64 10 36 61 ")) colnames(r) <- c("z","l","o") i want extract rows based on mutual column (in case z column), result
a 15 56 44 1 4 7 10 36 99 5 9 6 a new matrix.
any ideas how ?
just do:
> merge(x=m, y=r, by='z') z q f d h l o 1 10 36 99 5 9 6 36 61 2 15 56 44 1 4 7 56 64 to maintain columns in m:
> merge(x=r, y=m, by='z', sort=false)[colnames(m)] z q f d h 1 15 56 44 1 4 7 2 10 36 99 5 9 6 r matrix extract
Comments
Post a Comment