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 4271058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:23:25+00:00 2026-05-21T07:23:25+00:00

My problem, removing the specific purpose, seems like this: how to transform a combination

  • 0

My problem, removing the specific purpose, seems like this:
how to transform a combination like this:
first use combn(letters[1:4], 2) to calculate the combination

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a"  "a"  "a"  "b"  "b"  "c" 
[2,] "b"  "c"  "d"  "c"  "d"  "d" 

use each column to obtain another data frame:

   [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1    2    3    4   5     6

elements are obtained, for example: the first element, from the first column of the above dataframe

then How can i transform the above dataframe into a matrix, for example result, things like:

   a   b   c  d
a  0   1   2  3
b  1   0   4  5
c  2   4   0  6
d  3   5   6  0

the elements with same col and row names will have zero value where others corresponding to above value

  • 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-05-21T07:23:26+00:00Added an answer on May 21, 2026 at 7:23 am

    Here is one way that works:

    inputs <- letters[1:4]
    combs <- combn(inputs, 2)
    N <- seq_len(ncol(combs))
    nams <- unique(as.vector(combs))
    out <- matrix(ncol = length(nams), nrow = length(nams))
    out[lower.tri(out)] <- N
    out <- t(out)
    out[lower.tri(out)] <- N
    out <- t(out)
    diag(out) <- 0
    rownames(out) <- colnames(out) <- inputs
    

    Which gives:

    > out
      a b c d
    a 0 1 2 3
    b 1 0 4 5
    c 2 4 0 6
    d 3 5 6 0
    

    If I had to do this a lot, I’d wrap those function calls into a function.

    Another option is to use as.matrix.dist() to do the conversion for us by setting up a "dist" object by hand. Using some of the objects from earlier:

    ## Far easier
    out2 <- N
    class(out2) <- "dist"
    attr(out2, "Labels") <- as.character(inputs)
    attr(out2, "Size") <- length(inputs)
    attr(out2, "Diag") <- attr(out2, "Upper") <- FALSE
    out2 <- as.matrix(out2)
    

    Which gives:

    > out2
      a b c d
    a 0 1 2 3
    b 1 0 4 5
    c 2 4 0 6
    d 3 5 6 0
    

    Again, I’d wrap this in a function if I had to do it more than once.

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

Sidebar

Related Questions

Edit: This may be a 5.0-specific bug. (I'm on 5.0.83). Removing the innodb_log_file_size setting
I have a problem removing breakpoints that are set when I attach to a
Im having a problem with removing non-utf8 characters from string, which are not displaying
Problem: to see when computer makes approximation in mathematical calculations when I use Python
After recently installing Windows 7 Professional, I'm getting a strange problem removing a TableItem
I'm trying to implement Gauss-Newton optimization for a specific problem on iPhone ARM using
EDIT: for those who come here with a similar problem, now i know this
I'm extracting information from a webpage in Swedish. This page is using characters like:
i have a problem in removing radio buttons using, i have used the (.remove)
I'd like to know what people's best practices are for removing unused code. Personally

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.