繁体   English   中英

如何从相似度数据框中创建相似度矩阵?

[英]How do I create a similarity matrix from a similarity data frame?

我在网上找到了这个并将其与我的数据一起使用:

df <- data.frame(seasons = c("Season1","Season2","Season3","Season4"))
for(i in unique(df$seasons)) {
  df[[paste0(i)]] <- ifelse(df$seasons==i,1,0)
}

唯一的挑战是结果单元格中有一个 0,我想从具有如下数据排列的数据框中填充一个有意义的值:

S1 S2 价值
第1季 第2季 3
第三季 第1季 5
第2季 第三季 4

请注意一对中的一个季节如何在 S1 或 S2 出现。

例如,我需要填写 {row Season1; col Season 2} 以及我的矩阵中的 {col Season 1 and row Season 2} 为 3。

无论如何我可以这样做吗? 我尝试了一些事情,但决定向社区大声疾呼,以防万一我错过了一些简单的事情!

非常感谢!

分为三个步骤,决定重建原始矩阵并称其为 S:

# Make square matrix of zeros
rc <- length(unique(df[[1]]) ) # going to assume that number of unique values is same in both cols
S <- diag(1, rc,rc)

# Label rows and cols
dimnames(S) <- list( sort(unique(df[[1]])), sort( unique(df[[2]])) )

# Assign value to matrix positions based on values of df[[3]]

S[ data.matrix( df[1:2])  ] <-   # using 2 col matrix indexing
    df[[3]]

# -------
> S
        Season1 Season2 Season3
Season1       1       3       0
Season2       0       1       4
Season3       5       0       1

它现在是一个真正的矩阵而不是一个数据框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM