Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9056969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:19:59+00:00 2026-06-16T14:19:59+00:00

I want to transform the following data format (simplified representation): image1 image2 rating 1

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:20:00+00:00Added an answer on June 16, 2026 at 2:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to transform following (simplified) dataset, created by following code: structure(list(W1.1 = structure(c(1L,
I want to transform the dataset list, where the row is composed of different
Given the following sequence: normal %(b)BOLD% normal I want to transform this into the
I want to transform a List[Option[T]] into a Option[List[T]] . The signature type of
I have the following data structure in my database: LastName FirstName CourseName John Day
I have a list of objects that I want to transform into another list
I want to translate an XML file with data like the following: <FlatData> <Details1_Collection>
I have the following data Food $44 Transport $52 Rentals $102 ... I want
I have the following code, which I want to output xml data using the
I have a project in Java. I want transform it to .NET-project. How to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.