简体   繁体   中英

transforming dataset (similarity ratings)

I want to transform the following data format (simplified representation):

  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

Reproduced by:

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")

To a format where you get a sort of correlation matrix, where the first two columns figure as indicators, and ratings are the values:

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

Does any of you know of a function in R to do this?

I would rather use matrix indexing:

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

I don't like the <<- operator very much, but it works for this (naming your structure 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

Not as elegant as Karsten's solution, but it does not rely on the order of the rows, nor does it require that all combinations be present.

Here is one approach, where dat is the data frame as defined in the question

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

This works only if the rows are ordered as in the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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