2014-06-26 66 views
-2

請幫助重新排列行和cloumns 。 我有兩個表(墊和MAT1) 表1是墊如何重新排列R中的行和列

mat <- matrix("", 5, 7) 
mat[c(1),] <- sample(c("AA", "AG", "GG", "--"), 7, TRUE) 
mat[c(2),] <- sample(c("AA", "AT", "TT", "--"), 7, TRUE) 
mat[c(3),] <- sample(c("CC", "CG", "GG", "--"), 7, TRUE) 
mat[c(4,5),] <- sample(c("CC", "CG", "GG", "--"), 7, TRUE) 
colnames(mat)= c("Line_1", "Line_2", "Line_3", "Line_4", "Line_5", "Line_6", "Line_7") 
row.names(mat)= c("Marker_1", "Marker_2", "Marker_3", "Marker_4", "Marker_5") 

表2是MAT1

mat1<-matrix(c("1", "1","3","8","10", 2.3, 4.3, 45.5, 102.8, 74.8), nrow = 5) 
row.names(mat1)= c("Marker_1", "Marker_2", "Marker_3", "Marker_4", "Marker_5") 
colnames(mat1)= c("Linkage_Group", "Map_Position") 

我用cbind到兩個表

mat2 <- cbind(mat, mat1) 

然而結合我需要通過比較每條線來重新獲得數據,並用以下格式的比較附加列來得到結果。如果TEH標記數據匹配它是「單聲道,如果一個或兩個已經丟失它是‘誤’,如果不匹配它是‘聚’

Comparison   Marker Linkage_Group Map_Position LineX1 LineX2 Mon/Poly 
Line_1/Line_2  "Marker_1"  "1"  "2.3"  "AA"  "--"  Mis 
Line_1/Line_3  "Marker_1"  "1"  "2.3"  "AA"  "TT"  Poly 
Line_1/Line_4  "Marker_1"  "1"  "2.3"  "AA"  "TT"  Poly 
Line_1/Line_5  "Marker_1"  "1"  "2.3"  "AA"  "AT"  Poly 
Line_1/Line_6  "Marker_1"  "1"  "2.3"  "AA"  "--"  Mis 
Line_1/Line_7  "Marker_1"  "1"  "2.3"  "AA"  "AA"  Mono 

. 
. 
. 

將有上述實施例21個的組合。

這裏有很多R大師......請幫幫我!

回答

0

檢查這有助於:

列名Line的組合。 使用grep找到列名的格局

indx <- combn(colnames(mat2)[grep("Line", colnames(mat2))], 2) 

通過INDX列拆分列名INDX使每個列表元素是,如果需要兩列

res <- do.call(rbind, lapply(split(indx, col(indx)), function(x) { 
m1 <- mat2[, c("Linkage_Group", "Map_Position", x)] ##subset mat2 
#compare the concerned columns rowwise 
m2 <- cbind(Comparison = paste(x, collapse = "/"), Marker = row.names(m1), m1, 
    `Mon/Poly` = apply(m1[, 3:4], 1, function(x) ifelse(any(grepl('--', x)), "Mis", 
ifelse(x[1] == x[2], "Mono", "Poly")))) 
})) 

順序的組合

res1 <- res[order(res[,"Marker"], res[,"Linkage_Group"], res[,"Map_Position"]),] 

tail(res1,3) 
#  Comparison  Marker  Linkage_Group Map_Position Line_1 Line_2 
# Marker_5 "Line_5/Line_6" "Marker_5" "10"   "74.8"  "--" "--" 
# Marker_5 "Line_5/Line_7" "Marker_5" "10"   "74.8"  "--" "--" 
# Marker_5 "Line_6/Line_7" "Marker_5" "10"   "74.8"  "--" "--" 
#   Mon/Poly 
# Marker_5 "Mis" 
# Marker_5 "Mis" 
# Marker_5 "Mis" 

順便說一句:最好將它們存儲在data.frame中

+0

謝謝akrub!...你救了我的一天! – user3543621

+0

'user3543621'。沒問題......如果你對答案滿意,你可以接受它。 – akrun

+0

akrun:在代碼中存在一個小問題,因爲當兩行都具有' - '時它不會正確區分。它應該在最後一欄中被分類爲「錯誤」而不是「單聲道」 – user3543621