R:如何根据列名和行名合并两个矩阵?

发布于 2024-11-02 18:23:50 字数 891 浏览 0 评论 0原文

输入矩阵A

       column1   column2   column3   column4
row1   0         1         0         0
row2   0         0         -1        0
row3   1         0         0         -1

输入矩阵B

       column5   column6   column7   column8
row1   0         1         0         0
row2   0         0         -1        0
row4   1         0         0         -1

输出矩阵C

       column1   column2   column3   column4    column5   column6   column7   column8
row1   0         1         0         0          0         1         0         0
row2   0         0         -1        0          0         0         -1        0
row3   1         0         0         -1         0         0         0         0
row4   0         0         0         0          1         0         0         -1

备注:矩阵A 和矩阵B 的行名重叠。但是,所有列的名称都不同。

Input matrix A

       column1   column2   column3   column4
row1   0         1         0         0
row2   0         0         -1        0
row3   1         0         0         -1

Input matrix B

       column5   column6   column7   column8
row1   0         1         0         0
row2   0         0         -1        0
row4   1         0         0         -1

Output matrix C

       column1   column2   column3   column4    column5   column6   column7   column8
row1   0         1         0         0          0         1         0         0
row2   0         0         -1        0          0         0         -1        0
row3   1         0         0         -1         0         0         0         0
row4   0         0         0         0          1         0         0         -1

Remarks: matrix A and matrixB got overlapped row's name. However, all the columns' names are different.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2024-11-09 18:23:50

您可以使用 merge 来通过指定可选参数 byall 来完成此操作:

#Reading data
txt1 <- "column1   column2   column3   column4
row1   0         1         0         0
row2   0         0         -1        0
row3   1         0         0         -1
"

txt2 <- "column5   column6   column7   column8
row1   0         1         0         0
row2   0         0         -1        0
row4   1         0         0         -1
"
dat1 <- read.table(textConnection(txt1), header = TRUE)
dat2 <- read.table(textConnection(txt2), header = TRUE)

#Merge them together
merge(dat1, dat2, by = "row.names", all = TRUE)

将产生

Row.names column1 column2 column3 column4 column5 column6 column7 column8
1      row1       0       1       0       0       0       1       0       0
2      row2       0       0      -1       0       0       0      -1       0
3      row3       1       0       0      -1      NA      NA      NA      NA
4      row4      NA      NA      NA      NA       1       0       0      -1

如果您想将 NA 替换为零,则此操作应该工作:

#Assign to an object
zz <- merge(dat1, dat2, by = "row.names", all = TRUE)
#Replace NA's with zeros
zz[is.na(zz)] <- 0

You can use merge to do this by specifying the optional parameters by and all:

#Reading data
txt1 <- "column1   column2   column3   column4
row1   0         1         0         0
row2   0         0         -1        0
row3   1         0         0         -1
"

txt2 <- "column5   column6   column7   column8
row1   0         1         0         0
row2   0         0         -1        0
row4   1         0         0         -1
"
dat1 <- read.table(textConnection(txt1), header = TRUE)
dat2 <- read.table(textConnection(txt2), header = TRUE)

#Merge them together
merge(dat1, dat2, by = "row.names", all = TRUE)

Will yield

Row.names column1 column2 column3 column4 column5 column6 column7 column8
1      row1       0       1       0       0       0       1       0       0
2      row2       0       0      -1       0       0       0      -1       0
3      row3       1       0       0      -1      NA      NA      NA      NA
4      row4      NA      NA      NA      NA       1       0       0      -1

If you want to replace the NAs with zeros, this should work:

#Assign to an object
zz <- merge(dat1, dat2, by = "row.names", all = TRUE)
#Replace NA's with zeros
zz[is.na(zz)] <- 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文