2017-08-04 125 views
0

我有一個有300列的矩陣,其中行對應於基因計數,列對應於樣本名稱。 r命令列格式如下:當列名是字符串時,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 

我怎樣才能重新排序整個矩陣?

謝謝!

回答

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 
+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)];