1. 數(shù)據(jù)準(zhǔn)備
library('dplyr')
# 構(gòu)建數(shù)據(jù)集
df1 = sample(100,1000,replace = T) %>% matrix(100,10,byrow = F) %>% data.frame()
df2 = sample(100,1000,replace = T) %>% matrix(100,10,byrow = F) %>% data.frame()
#定義列名
names(df1) = paste('Smp', seq(10), sep = '')
names(df2) = paste('Smp', sample(10,10), sep = '')
#簡(jiǎn)單可視化
par(mfrow = c(1,2))
boxplot(df1,main = 'df1');boxplot(df2,main = 'df2')
par(mfrow = c(1,1))
2. 生成距離矩陣,
多個(gè)矩陣的列名及順序必須一致
##調(diào)整樣品順序使一致
df2 = df2[,names(df1)]
dst1 = df1 %>% t() %>% dist(method = 'euclidean',upper = T,diag = 0) %>% as.matrix()
dst2 = df2 %>% t() %>% dist(method = 'euclidean',upper = T,diag = 0) %>% as.matrix()
3. 定義簡(jiǎn)單函數(shù)
mtrx2cols = function(m1,m2,val1,val2){
lt = lower.tri(m1) #獲取下半角為T(mén)RUE嗓蘑,上半角為FALSE的同維度矩陣;
res = data.frame(row = row(m1,as.factor = T)[lt], #返回矩陣m1的下半角元素對(duì)應(yīng)的行名
col = col(m1,as.factor = T)[lt], #返回矩陣m1的下半角對(duì)應(yīng)的列名
val1 = m1[lt], val2= m2[lt]) #按列依次獲取矩陣下半角的元素
names(res)[3:4] = c(val1,val2) #對(duì)后兩列重命名扼倘,支持多個(gè)矩陣合并
return(res)
}
-
row()
和col()
函數(shù)用于取矩陣元素的行或列下標(biāo)矩陣遇绞。
row(as.factor = T)
可以理解為將行名向右平移填充整個(gè)矩陣
col(as.factor = T)
可以理解為將列名向下平移填充整個(gè)矩陣
-
- 該函數(shù)支持多個(gè)矩陣的轉(zhuǎn)換
執(zhí)行轉(zhuǎn)換
res = mtrx2cols(dst1,dst2,'Eco','logy')