2013-04-07 138 views
0

我有兩個矩陣(例如A和B)。我想基於A的第一列的順序來提取B的列:按列連接兩個矩陣並提取子矩陣

例如

矩陣A

name score 
a 0.1 
b 0.2 
c 0.1 
d 0.6 

矩陣B

a d b c g h 
0.1 0.2 0.3 0.4 0.6 0.2 
0.2 0.1 0.4 0.7 0.1 0.1 
... 

我想矩陣B到看起來像這樣最後

matrix B_modified

a b c d 
0.1 0.3 0.4 0.2 
0.2 0.4 0.7 0.1 

這可以在perl或R中完成嗎?非常感謝

+1

像這樣的事情? 'b [,a [,「name」]]' – Arun 2013-04-07 10:35:51

+0

嗨@阿倫,非常感謝。我得到了一些輸出,但並不完全按照我希望的順序。我正在檢查a [,「name」]的輸出,並且列出名稱並添加'13500級別:a b ... z'。這可能是不正確分類名稱的原因嗎? – user1007742 2013-04-07 10:58:31

+0

是的,只需要:'a [,「name」] < - as.character(a [,「name」])'執行之前提到的步驟。 – Arun 2013-04-07 11:02:48

回答

2

我不知道你面臨什麼問題。以下是我如何做到的。

## get data as matrix 
a <- read.table(header=TRUE, text="name score 
a 0.1 
b 0.2 
c 0.1 
d 0.6", stringsAsFactors=FALSE) # load directly as characters 

b <- read.table(header=TRUE, text="a d b c g h 
0.1 0.2 0.3 0.4 0.6 0.2 
0.2 0.1 0.4 0.7 0.1 0.1", stringsAsFactors=FALSE) 

a <- as.matrix(a) 
b <- as.matrix(b) 

現在子集,讓您的最終結果:

b[, a[, "name"]] 
#  a b c d 
# [1,] 0.1 0.3 0.4 0.2 
# [2,] 0.2 0.4 0.7 0.1 
+0

非常感謝。現在排序。 – user1007742 2013-04-08 22:04:05

+0

@ user1007742,你似乎很困惑,選擇一個答案:) – Arun 2013-04-08 22:12:12

+0

他們都爲我工作得很好,我以爲我可以選擇兩個作爲答案,但系統只讓我選擇一個:)所以我把它留下,因爲它是 – user1007742 2013-04-09 12:23:11

2

錯誤:

[.data.frame(b, , a[, "name"]) : undefined columns selected 

意味着你試圖獲得b定義的列非,但存在a$name。一種解決方案是使用intersectcolnames(b)。這也會將該因子轉換爲一個字符串,並獲得正確的順序。如果您的數據作爲R數據結構源自那麼這將是有害的出口,並使用Perl解決這個問題

b <- read.table(text=' 
a d b c 
0.1 0.2 0.3 0.4 
0.2 0.1 0.4 0.7',header=TRUE) 

a <- read.table(text='name score 
a 0.1 
z 0.5 
c 0.1 
d 0.6',header=TRUE) 

b[, intersect(a[, "name"],colnames(b))] 


    a c d 
1 0.1 0.4 0.2 
2 0.2 0.7 0.1 
+0

非常感謝。現在排序。 – user1007742 2013-04-08 22:04:35

2

b[, intersect(a[, "name"],colnames(b))] ## the order is important here 

例如,我與此數據進行測試。但是,如果您的文本文件看起來像您所顯示的數據,那麼這裏就是您的Perl解決方案。

我已經在空間上分割輸出。如有必要,可以非常簡單地進行更改。

use strict; 
use warnings; 
use autodie; 

sub read_file { 
    my ($name) = @_; 
    open my $fh, '<', $name; 
    my @data = map [ split ], <$fh>; 
    \@data; 
} 

my $matrix_a = read_file('MatrixA.txt'); 
my @fields = map $matrix_a->[$_][0], 1 .. $#$matrix_a; 

my $matrix_b = read_file('MatrixB.txt'); 
my @headers = @{$matrix_b->[0]}; 
my @indices = map { 
    my $label = $_; 
    grep $headers[$_] eq $label, 0..$#headers 
} @fields; 

for my $row (0 .. $#$matrix_b) { 
    print join(' ', map $matrix_b->[$row][$_], @indices), "\n"; 
} 

輸出

a b c d 
0.1 0.3 0.4 0.2 
0.2 0.4 0.7 0.1