I have a pairwise correlation dataframe which looks like this (this is just the head) :
var1 var2 corr
1 OTU3978 UniRef90_A0A010P3Z8 0.846
2 OTU4011 UniRef90_A0A010P3Z8 0.855
3 OTU4929 UniRef90_A0A010P3Z8 0.829
4 OTU4317 UniRef90_A0A011P550 0.850
5 OTU4816 UniRef90_A0A011P550 0.807
6 OTU3902 UniRef90_A0A011QPQ2 0.836
7 OTU3339 UniRef90_A0A011RKI6 0.835
8 OTU1359 UniRef90_A0A011RLA7 0.801
9 OTU2085 UniRef90_A0A011RLA7 0.843
10 OTU3542 UniRef90_A0A011RLA7 0.866
I would like to get the correlation matrix from this dataframe I tried :
library(igraph)
G <- graph.data.frame(df,directed=FALSE)
A <-as_adjacency_matrix(G,names=TRUE,sparse=FALSE,attr="corr",type='lower')
But it didn't work.
There are a few ways to do this. One approach is the acast
you mentioned, but you can also use spread
from tidyr
library(tidyr)
var1 <- c("A", "B", "C", "D")
var2 <- c("E", "E", "F", "F")
corr <- c(0.2, 0.3, 0.4, 0.5)
df <- data.frame(var1, var2, corr)
df
var1 var2 corr
1 A E 0.2
2 B E 0.3
3 C F 0.4
4 D F 0.5
df <- spread(df, var2, corr)
df
var1 E F
1 A 0.2 NA
2 B 0.3 NA
3 C NA 0.4
4 D NA 0.5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.