重新排序R中的行和列

时间:2017-01-27 13:31:35

标签: r

我知道之前已经回答过,但是, 给出一个如下所示的相关矩阵:

V  A    B    C     D 
A  1    0.3  0.1   0.4
B  0.2  1    0.4   0.3
C  0.1  0    1     0.9
D  0.3  0.3  0.1   1

可以按如下方式加载到R中:

corr.matrix <- read.table("path/to/file", sep = '\t', header = T)
rownames(corr.matrix) <- corr.matrix$V
corr.matrix <- corr.matrix[, 2:ncol(corr.matrix)]

基于2个其他文件来决定要绘制哪些行和列(因为有些对我来说不感兴趣),我想重新排列行和列中2个单独文件的命令。

例如:

cols_order.txt                      
C
D
E
B
A
...

rows.txt
D
E
Z
B
T
A
...

我读了这样的其他2个文件:

rows.order <- ("rows_order.txt", sep = '\n', header=F)
colnames(rows.order) <- "Variant"

cols.order <- ("cols_order.txt", sep = '\n', header=F)
colnames(cols.order) <- "Variant"

在这一步之后,我这样做:

corr.matrix <- corr.matrix[rows.order$Variant, cols.order$Variant]

我不想要绘制的值已成功删除,但订单会被扰乱。我怎样才能解决这个问题?

正确读取.order数据集(我检查了3次)。

1 个答案:

答案 0 :(得分:3)

这是您问题的潜在解决方案。我试图根据你的问题重新创建一个小型的data.frame。这里的关键是match函数以及R中的一些基本子集/过滤技术:

## Re-create your example:
V <- data.frame(
  A = c(1 ,  0.3, 0.1 , 0.4),
  B = c(0.2, 1 ,  0.4 , 0.3),
  C = c(0.1, 0 ,  1  ,  0.9),
  D = c(0.3, 0.3, 0.1 , 1)
) #matrix() also ok
rownames(V) <- LETTERS[1:4]

## Reorder using `match` function
## Needs to be in data.frame form
## So use as.data.frame() if needed

## Here, I don't have the text file
## So if you want to load in txt files specifying rows columns
## Use `read.csv` or `read.table to load
## And then store the relevant info into a vector as you did

col_order <- c("C","D","E","B","A")
col_order_filtered <- col_order[which(col_order %in% colnames(V))]
rows <- c("D","E","Z","B","T","A")
## Filter rows IDs, since not all are present in your data
row_filtered <- rows[rows %in% rownames(V)]

V1 <- V[match(rownames(V), row_filtered), match(colnames(V), col_order_filtered)]
V1 <- V1[-which(rownames(V1)=="NA"), ]
V1

##     D   C   A   B
## C 0.1 1.0 0.1 0.4
## B 0.3 0.0 0.3 1.0
## A 0.3 0.1 1.0 0.2

或者,如果您对dplyr包和语法感到满意,可以使用它并且通常很方便:

## Continued from previous code
library(dplyr)
V2 <- V %>%
  select(C, D, B, A, everything()) %>%
  slice(match(rownames(V), row_filtered))
rownames(V2) <- row_filtered
V2
##     C   D   B   A
## D 1.0 0.1 0.4 0.1
## B 0.0 0.3 1.0 0.3
## A 0.1 0.3 0.2 1.0

希望有所帮助。