I have been trying to do something I know is so simple! I've read and tried everything I can find but am still struggling with this very simple task.
I have two character matrices that have some matching columns and I need to combine them without losing the mismatched columns from 1 matrix. I have tried merge, left join, and intersect and am still not getting the desired result.
Here's matrix 1:
mat1 <- matrix( nrow = 1, ncol = 6077, data = "_" )
colnames(mat1)<- sprintf( "Cd", 1:6077)
mat1[,1:909] <- "5' UTR"
mat1[,910:1923] <- "ORF1"
mat1[,1990:5814] <- "ORF2"
mat1[,49:420] <- "CPG"
mat1[,5815:6077] <- "3' UTR"
mat1[,211:225] <- "RXRA::VDR"
Column names in matrix 1 go from C0001 to C6077 and matrix 2 has some of the same column names and some different ones. I need to copy/paste the values from matrix 1 to matrix 2 if the columns match, while keeping all of the columns in matrix 2.
mat2 <- matrix(nrow =1, ncol = 892, data = "_")
colnames(m2) <- colnames(aln)
My output needs to look something like:
> mat3
G0242 G0243 C0001 C0002 C0003 C0004 C0005
"_" "_" "5'UTR" "5'UTR" "5'UTR" "5'UTR" "5'UTR"
I tried to left join and got an error
"Error in UseMethod("left_join") : no applicable method for 'left_join' applied to an object of class "c('matrix', 'array', 'character')"
And it seems I'm unable to just assign the values I want:
m2[,"C0001":"C0909"] <- "5' UTR"
Error in "C0001":"C0909" : NA/NaN argument
In addition: Warning messages:
And I've tried a bunch of varieties of merge, cbind, and rbind, but nothing has given me the right result.
Someone, some sanity, please.
CodePudding user response:
Perhaps something like this:
for(col in intersect(colnames(mat1), colnames(mat2))) {
mat2[,col] <- mat1[,col]
}