当列名为字符串时,在R中排序矩阵

时间:2017-08-04 19:08:58

标签: r sorting matrix

我有一个包含300列的矩阵,其中行对应于基因计数,列对应于样本名称。 按以下格式排列R列:

sample59 sample6 sample60 sample61 sample62 sample63 sample64 sample65
[1,]  2       679.567      361        2       17        0        0        0
[2,]  0       0.000        0        0        0        0        0        0
[3,]  0       0.000        0        0        0        0        0        0
[4,]  0       0.000        0        0        0        0        0        0
[5,]  0       0.000    0        0        0        0        0        0
[6,]  0       0.000    0        0        0        0        0        0

我想像这样格式化矩阵:

sample6 sample59 sample60 sample61 sample62 sample63 sample64 sample65
[1,]  679.567       2      361        2       17        0        0        0
[2,]    0.000       0        0        0        0        0        0        0
[3,]    0.000       0        0        0        0        0        0        0
[4,]    0.000       0        0        0        0        0        0        0
[5,]    0.000       0        0        0        0        0        0        0
[6,]    0.000       0        0        0        0        0        0        0

如何重整整个矩阵?

谢谢!

2 个答案:

答案 0 :(得分:2)

# get column names of your dataframe (called df) and remove the "sample" word
n = sub("sample", "", names(df));
# and convert n to numbers (as they are, right?)
n = as.numeric(n)

# reorder your data.frame column depending on the n sorting
df = df[, order(n)];

答案 1 :(得分:1)

首先,让您的列名读取。

cnames <- scan(what = "character", text ="
sample59 sample6 sample60 sample61 sample62 sample63 sample64 sample65")

现在让我们用一些伪矩阵来解决列顺序问题。

library(stringr)

mat <- matrix(1:80, ncol = 8)
colnames(mat) <- cnames

icol <- str_order(sub("sample", "", cnames), numeric = TRUE)
mat2 <- mat[, icol]
mat2