r - Subsetting one matrix based in another matrix -
i select r
based on g
strings obtain separated outputs equal dimensions. inputs:
r <- 'pr_id sample1 sample2 sample3 ax-1 100 120 130 ax-2 150 180 160 ax-3 160 120 196' r <- read.table(text=r, header=t) g <- 'pr_id sample1 sample2 sample3 ax-1 ab aa aa ax-2 bb ab na ax-3 bb ab aa' g <- read.table(text=g, header=t)
this expected outputs:
aa <- 'pr_id sample1 sample2 sample3 ax-1 na 120 130 ax-2 na na na ax-3 na na 196' aa <- read.table(text=aa, header=t) ab <- 'pr_id sample1 sample2 sample3 ax-1 100 na na ax-2 na 180 na ax-3 na 120 na' ab <- read.table(text=ab, header=t) bb <- 'pr_id sample1 sample2 sample3 ax-1 na na na ax-2 150 na na ax-3 160 na na' bb <- read.table(text=bb, header=t)
some idea perform it?
we subset 'g' 2nd column, convert matrix, , split sequence values in that, create new matrix na ("g1") , using index, replace values corresponds "r" dataset values.
lapply(split(seq_along(as.matrix(g[-1])), as.matrix(g[-1])), function(x) { g1 <- matrix(na, ncol=ncol(g)-1, nrow=nrow(g), dimnames=list(null, names(g)[-1])) g1[x] <- as.matrix(r[-1])[x] data.frame(pr_id=r$pr_id, g1) }) #$aa # pr_id sample1 sample2 sample3 #1 ax-1 na 120 130 #2 ax-2 na na na #3 ax-3 na na 196 #$ab # pr_id sample1 sample2 sample3 #1 ax-1 100 na na #2 ax-2 na 180 na #3 ax-3 na 120 na #$bb # pr_id sample1 sample2 sample3 #1 ax-1 na na na #2 ax-2 150 na na #3 ax-3 160 na na
Comments
Post a Comment