按名称组合矩阵 [英] combining matrix by name

查看:79
本文介绍了按名称组合矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组名为gof_1_1gof_1_2,.....,gof_1_24的矩阵.我想将所有这些都合并到沿着列的一个矩阵中.因此,请使用以下代码

I have a set of matrix named gof_1_1, gof_1_2, ..... ,gof_1_24. I want to combine all of them in to one matrix along the columns. So am using the following code

do.call(cbind,mget(ls(pattern = paste("gof",1,"[0-9]",sep="_"), globalenv())))

它组合了矩阵,但是问题在于它们不按顺序排列.它们像这样gof_1_1gof_1_11gof_1_12,.....,gof_1_19gof_1_2gof_1_21等.所以我如下所示编辑了ls()

It combines the matrix, but problem is they are not in an order. They go like this gof_1_1 , gof_1_11, gof_1_12, ..... , gof_1_19, gof_1_2, gof_1_21 and so on. So I edited the ls() as shown below

ls(pattern = paste("gof",1,"[0-9][0-9]",sep="_"),globalenv())

现在按顺序,但是它从gof_1_10开始到gof_1_25.缺少gof_1_1gof_1_9.知道如何编辑以上代码以按顺序调用所有矩阵吗?

Now it's in order,But it starts from gof_1_10, to gof_1_25. Missing gof_1_1 to gof_1_9. Any idea how to edit the above one to call all the matrix in order?

推荐答案

您可以这样做:

do.call(cbind, mget(paste0("gof_1_", 1:24)))

否则,类似以下的事情

mat.names    <- ls(pattern = paste("gof", 1, "[0-9]", sep="_"), globalenv())
mat.idx      <- as.integer(gsub(".*_", "", mat.names))
sorted.names <- mat.names[order(mat.idx)]
do.call(cbind, mget(sorted.names))

这篇关于按名称组合矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆