2012-12-29 33 views
2

我要变换的以下数据格式(简化表示):转化数据集(相似性评级)

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 

Doe你有没有知道R中的一个函数来做到这一点?

回答

3

我不喜欢<<-运营商非常多,但它适用于这个(命名您的结构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的解决方案,但它不依赖的顺序行,也不要求所有组合都存在。

1

这是一种方法,其中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 

这只有行排序作为问题定义的数据帧。

+0

如果每个组合被称为是本正好一次,但行的顺序不正确,该解决方案可以应用于'DAT [顺序(数据[ ,1],DAT [1,2]),]'。 –

4

我宁愿使用矩阵索引:

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 
+0

正确的做法。 –