【问题标题】:transforming dataset (similarity ratings)转换数据集(相似度评级)
【发布时间】:2012-12-30 02:54:24
【问题描述】:

我想转换以下数据格式(简化表示):

  image1 image2 rating
1      1      2      6
2      1      3      5
3      1      4      7
4      2      3      3
5      2      4      5
6      3      4      1

转载:

structure(list(image1 = c(1, 1, 1, 2, 2, 3), image2 = c(2, 3, 
4, 3, 4, 4), rating = c(6, 5, 7, 3, 5, 1)), .Names = c("image1", 
"image2", "rating"), row.names = c(NA, -6L), class = "data.frame")

转换为一种格式,您可以获得一种相关矩阵,其中前两列作为指标,评级是值:

   1  2  3  4
1 NA  6  5  7
2  6 NA  3  5
3  5  3 NA  1
4  7  5  1 NA

你们中有人知道 R 中有一个函数可以做到这一点吗?

【问题讨论】:

    标签: r database-design


    【解决方案1】:

    我宁愿使用矩阵索引:

    N <- max(dat[c("image1", "image2")])
    out <- matrix(NA, N, N)
    out[cbind(dat$image1, dat$image2)] <- dat$rating
    out[cbind(dat$image2, dat$image1)] <- dat$rating
    
    #      [,1] [,2] [,3] [,4]
    # [1,]   NA    6    5    7
    # [2,]    6   NA    3    5
    # [3,]    5    3   NA    1
    # [4,]    7    5    1   NA
    

    【讨论】:

      【解决方案2】:

      我不太喜欢&lt;&lt;- 运算符,但它适用于这个(命名你的结构s):

      N <- max(s[,1:2])
      m <- matrix(NA, nrow=N, ncol=N)
      apply(s, 1, function(x) { m[x[1], x[2]] <<- m[x[2], x[1]] <<- x[3]})
      
       > m
           [,1] [,2] [,3] [,4]
      [1,]   NA    6    5    7
      [2,]    6   NA    3    5
      [3,]    5    3   NA    1
      [4,]    7    5    1   NA
      

      不如 Karsten 的解决方案优雅,但它不依赖于行的顺序,也不要求所有组合都存在。

      【讨论】:

        【解决方案3】:

        这是一种方法,其中dat 是问题中定义的数据框

        res <- matrix(0, nrow=4, ncol=4) # dim may need to be adjusted
        ll <- lower.tri(res, diag=FALSE)
        res[which(ll)] <- dat$rating
        res <- res + t(res)
        diag(res) <- NA
        

        仅当行按问题中的顺序排列时才有效。

        【讨论】:

        • 如果已知每个组合只出现一次,但行的顺序不正确,则可以将此解决方案应用于dat[order(data[,1],dat[,2]),]
        猜你喜欢
        • 1970-01-01
        • 2019-05-21
        • 2015-09-30
        • 2017-12-08
        • 2022-08-06
        • 2016-02-01
        • 2020-03-14
        • 1970-01-01
        • 2020-08-27
        相关资源
        最近更新 更多