簡體   English   中英

將列名和行名與另一個數據框中的列和值匹配

[英]Match column and row names to column and values in another data frame

我有兩個數據框如下:

x<-data.frame("Trait1" =c(1,1,0,1),
          "Trait2"=c(1,NA,1,1),
          "Trait3" =c(0,1,0,1))
rownames(x)<-c("A","B","C","D") 

y <- matrix(c("A","A","B","C","D","C"), 
           nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("individual1", "individual2"), 
                                                             c("Trait1","Trait2","Trait3")))

這樣:

X

  Trait1 Trait2 Trait3
A      1      1      0
B      1     NA      1
C      0      1      0
D      1      1      1

         Trait1 Trait2 Trait3
individual1 "A"    "A"    "B"   
individual2 "C"    "D"    "C"  

我需要將 x 的行名稱與 y 中的值以及兩個數據框中的列名稱進行匹配,以獲取每個人的值,如下所示:

         Trait1 Trait2 Trait3
individual1   1      1      1   
individual2   0      1      0  

任何建議將不勝感激。 謝謝。

tidyverse 的一個可能解決方案:只需使用有關每個治療編號和治療名稱的信息連接表,因此第一步是將兩個數據集轉換( gather )為一個通用形式,其中治療編號和治療都是列, 不是列名或行名。

library(dplyr)
library(tidyr)

x %>% mutate(v=rownames(.)) %>% 
  gather(k,w,-v) -> x1
y %>% as.data.frame(stringsAsFactors=FALSE) %>% mutate(ID=rownames(.)) %>%
  gather(k,v,-ID) %>% 
  inner_join(x1,by=c("k","v")) %>% 
  select(-v) %>% spread(k,w)

#           ID Trait1 Trait2 Trait3
#1 individual1      1      1      1
#2 individual2      0      1      0

我不太喜歡我的解決方案,我覺得應該有更好的方法來做到這一點,但目前這應該可行(雖然它不是那么好)。

這是代碼:

t(sapply(1:nrow(y),function(i) sapply(1:ncol(y),function(j) x[match(y[i,],rownames(x)) 
[j],j])))

輸出:

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    0    1    0

解釋:

match(y[i,],rownames(x))

以上將 y 的第 i 行的每一列與 x 的行名匹配。 對於 i=1,結果是:

[1] 1 1 2

這個向量中的每個元素都是我們將使用的 x 的行。 現在我們只需要將它匹配到 y 的列(向量的順序對應於 y 的列,即元素 1 對應於列 1(trait1),元素 2 對應於列 2(trait2))。所以我們應用到每個y 列如下:

對於 i=1

sapply(1:ncol(y),function(j) x[match(y[i,],rownames(x))[j],j])
#[1] 1 1 1

這是新矩陣的第一行,現在我們只需將其應用於 y 的每一行以獲得新矩陣的其他行:

t(sapply(1:nrow(y),function(i) sapply(1:ncol(y),function(j)                                                                
    x[match(y[i,],rownames(x))[j],j])))

*注意我采用轉置,因為 sapply 每列返回它。

無論如何,這可行,您可以將新矩陣命名為與 y 相同的名稱,但解決方案有點復雜,因為我認為應該更簡單,因此請檢查您是否可以改進代碼。 如果您可以更好地使用以下語句,則可能不需要 sapply:

i=1
match(y[i,],rownames(x))

這是一個建議:

#make table of row coordinates
coordinaterow<-y

#make table of col coordinates
coordinatecol<-matrix(colnames(y), 
                      nrow=nrow(y), 
                      ncol=ncol(y), 
                      byrow=TRUE)

#Use coordinates in mapply function to produce the final table.
finalresult<-y  

finalresult[]<-mapply(function(r,c)
  x[r,c],
  coordinaterow,
  coordinatecol,
  SIMPLIFY = TRUE)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM