[英]How to convert two factors to adjacency matrix in R?
I have a data frame with two columns (key and value) where each column is a factor: 我有一个包含两列(键和值)的数据框,其中每一列都是一个因素:
df = data.frame(gl(3,4,labels=c('a','b','c')), gl(6,2))
colnames(df) = c("key", "value")
key value
1 a 1
2 a 1
3 a 2
4 a 2
5 b 3
6 b 3
7 b 4
8 b 4
9 c 5
10 c 5
11 c 6
12 c 6
I want to convert it to adjacency matrix (in this case 3x6 size) like: 我想将其转换为邻接矩阵(在这种情况下为3x6大小),例如:
1 2 3 4 5 6
a 1 1 0 0 0 0
b 0 0 1 1 0 0
c 0 0 0 0 1 1
So that I can run clustering on it (group keys that have similar values together) with either kmeans or hclust. 这样我就可以使用kmeans或hclust在其上运行集群(具有相似值的组键)。
Closest that I was able to get was using model.matrix( ~ value, df)
which results in: 我能够获得的最接近模型是使用model.matrix( ~ value, df)
:
(Intercept) value2 value3 value4 value5 value6
1 1 0 0 0 0 0
2 1 0 0 0 0 0
3 1 1 0 0 0 0
4 1 1 0 0 0 0
5 1 0 1 0 0 0
6 1 0 1 0 0 0
7 1 0 0 1 0 0
8 1 0 0 1 0 0
9 1 0 0 0 1 0
10 1 0 0 0 1 0
11 1 0 0 0 0 1
12 1 0 0 0 0 1
but results aren't grouped by key yet. 但结果尚未按关键字分组。
From another side I can collapse this dataset into groups using: 另一方面,我可以使用以下方法将此数据集折叠成组:
aggregate(df$value, by=list(df$key), unique)
Group.1 x.1 x.2
1 a 1 2
2 b 3 4
3 c 5 6
But I don't know what to do next... 但是我不知道下一步该怎么做...
Can someone help to solve this? 有人可以帮助解决这个问题吗?
An easy way to do it in base
R: 在base
R中执行此操作的简单方法:
res <-table(df)
res[res>0] <-1
res
value
#key 1 2 3 4 5 6
# a 1 1 0 0 0 0
# b 0 0 1 1 0 0
# c 0 0 0 0 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.