-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistToMatrix.R
More file actions
38 lines (36 loc) · 939 Bytes
/
listToMatrix.R
File metadata and controls
38 lines (36 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#long_dat <- data.frame("key1" = c("a", "b", "c","a", "b", "c","a", "b", "c"),"key2" = c("a", "a", "a", "b", "b", "b","c", "c", "c"), "values" = 1:9)
listToMatrix <- function(long_dat, symmetric = TRUE)
{
possible.keys <- unique(c(long_dat[,1],long_dat[,2]))
out.mat <- matrix(NA, nrow = length(possible.keys), ncol = length(possible.keys))
rownames(out.mat) <- possible.keys
colnames(out.mat) <- possible.keys
for(i in 1:nrow(long_dat))
{
r <- long_dat[i,1]
c <- long_dat[i,2]
out.mat[r,c] <- long_dat[i,3]
}
if(symmetric & any(is.na(out.mat)))
{
#fill in missing
for(i in 1:nrow(out.mat))
{
for(j in 1:ncol(out.mat))
{
if(is.na(out.mat[i,j]))
{
out.mat[i,j] <- out.mat[j,i]
}
}
}
}
if(symmetric)
{
if(!isSymmetric(out.mat))
{
message("Matrix doesn't appear to be symmetric. Sorry pal.")
}
}
return(out.mat)
}