1. 输入3列
只要前三列,第一列是行名,第二列是列名,第三列为值。
> head(df.net2.order)
from to strength type
12439 CSTF2 ENST0000056844 -0.6859788 neg
12015 CSTF2 ENST0000056190 -0.5153181 neg
11208 CSTF2 GAPDH -0.4570489 neg
2. 输出数据框
行为基因调控因子,列为基因表达,值为相关系数。
> df.net2.df=df3toMatrix(df.net2.order)
> dim(df.net2.df)
[1] 27 4022
> df.net2.df[df.net2.df==0]=NA
> df.net2.df[1:4,1:5]
ENST0000056844 ENST0000056190 GAPDH ENST0000063431 ARL6
CSTF2 -0.6859788 -0.5153181 -0.4570489 -0.4380417 -0.4351847
NUDT21 NA -0.4719560 -0.4080007 NA -0.4125685
CPSF3 -0.4883905 -0.3955025 -0.4318929 NA -0.4517824
CPSF1 NA -0.3722944 -0.3625508 NA -0.3016818
3. 转换函数
# from 3 columns to matrix: col1-row, col2-col, col2-value
df3toMatrix=function(df3){
rows.id=df3[,1] |> unique()
cols.id=df3[,2] |> unique()
output=data.frame(matrix(0, nrow=length(rows.id), ncol=length(cols.id)))
rownames(output)=rows.id
colnames(output)=cols.id
for(i in 1:nrow(df3)){
output[df3[i, 1], df3[i, 2]]=df3[i,3]
}
output
}