简体   繁体   English

按常用列名连接字符矩阵

[英]Join character matrices by common column names

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.我有两个具有一些匹配列的字符矩阵,我需要将它们组合起来而不丢失 1 个矩阵中不匹配的列。 I have tried merge, left join, and intersect and am still not getting the desired result.我尝试了合并、左连接和相交,但仍然没有得到想要的结果。

Here's matrix 1:这是矩阵 1:

mat1 <- matrix( nrow = 1, ncol = 6077, data = "_" )
colnames(mat1)<- sprintf( "C%04d", 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.矩阵 1 go 从 C0001 到 C6077 的列名与矩阵 2 的列名有的相同,有的不同。 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.如果列匹配,我需要将值从矩阵 1 复制/粘贴到矩阵 2,同时将所有列保留在矩阵 2 中。

mat2 <- matrix(nrow =1, ncol = 892, data = "_")
colnames(m2) <- colnames(aln)

My output needs to look something like:我的 output 需要看起来像这样:

> 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.我已经尝试了多种合并、cbind 和 rbind,但都没有给我正确的结果。

Someone, some sanity, please.请有人,一些理智。

Perhaps something like this:也许是这样的:

for(col in intersect(colnames(mat1), colnames(mat2))) {
  mat2[,col] <- mat1[,col]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM