2017-01-27 2 views
1

나는이 전에 대답되었지만, 은 다음과 같습니다 상관 행렬을 부여 알고 다음과 같이 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 

:

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 개의 다른 파일을 기반으로합니다 (일부 파일에는 관심이 없기 때문에). 두 개의 개별 파일이 어떻게 지정되는지 행과 열을 다시 정렬하려고합니다. 예를 들어

는 :

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" 

그리고이 단계 후에 나는이 수행 :

cols_order.txt      
C 
D 
E 
B 
A 
... 

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

는이 같은 그 다른 두 파일을 읽을

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

값을 그 나는 돈 음모를 꾸미고 싶지는 않지만 성공적으로 제거되지만 주문은 뒤죽박죽입니다. 이 문제를 어떻게 해결할 수 있습니까?

.order 데이터 세트가 올바르게 읽혔습니다 (3 번 확인).

답변

3

여기에 귀하의 질문에 대한 잠재적 인 해결책이 있습니다. 귀하의 질문에 따라 작은 크기의 data.frame을 다시 만들려고했습니다. 당신이 dplyr 패키지와 구문을 사용하여 편안 경우 사용할 수 있습니다, 또는

## 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 

과 자주 편리합니다 : 여기서 핵심은 match 기능뿐만 아니라 R의 기본적인 부분 집합/필터링 기술이다

## 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 

희망이 있습니다.