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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:13:06+00:00 2026-05-25T12:13:06+00:00

Input file: df1 <- data.frame(row.names=c(w,x,y,z), A=c(0,0,0,0), B=c(0,1,0,0), C=c(1,0,1,0), D=c(1,1,1,1)) A B C D w

  • 0

Input file:

df1 <- data.frame(row.names=c("w","x","y","z"), 
                  A=c(0,0,0,0),
                  B=c(0,1,0,0), 
                  C=c(1,0,1,0), 
                  D=c(1,1,1,1))

  A B C D
w 0 0 1 1
x 0 1 0 1
y 0 0 1 1
z 0 0 0 1

I want to apply an equation i.e. multiply row w to row x to get the pairwise value for w-x pair, as follows:

      A B C D
    w 0 0 1 1
X   x 0 1 0 1
--------------
   wx 0 0 0 1

to get row-wise analysis for w-x, w-y, w-y, w-z, x-y, x-z, y-z. and generate a new dataframe with 6 columns (two row names followed by the multiplied values).

That’s

w x 0 0 0 1
w y 0 0 1 1
w z 0 0 0 1
x y 0 0 0 1
x z 0 0 0 1
y z 0 0 0 1

Thanks.

  • 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-25T12:13:07+00:00Added an answer on May 25, 2026 at 12:13 pm

    If you don’t want the combo names in the resulting object, then we can combine elements of @DWin’s and @Owen’s Answers to provide a truly vectorised approach to the problem. (You can add the combination names as row names with one extra step at the end.)

    First, the data:

    dat <- read.table(con <- textConnection("  A B C D
    w 0 0 1 1
    x 0 1 0 1
    y 0 0 1 1
    z 0 0 0 1
    "), header=TRUE)
    close(con)
    

    Take the combn() idea from @DWin’s Answer but use it on the row indices of dat:

    combs <- combn(seq_len(nrow(dat)), 2)
    

    The rows of combs now index the rows of dat that we want to multiply together:

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

    Now we take the idea @Owen showed, namely dat[i, ] * dat[j, ] with i and j being the first and second rows of combs respectively. We convert to a matrix with data.matrix() as this will be more efficient for large objects, but the code will work with dat as a data frame too.

    mat <- data.matrix(dat)
    mat[combs[1,], ] * mat[combs[2,], ]
    

    which produces:

    > mat[combs[1,], ] * mat[combs[2,], ]
      A B C D
    w 0 0 0 1
    w 0 0 1 1
    w 0 0 0 1
    x 0 0 0 1
    x 0 0 0 1
    y 0 0 0 1
    

    To see how this works, note that mat[combs[k,], ] produces a matrix with various rows repeated in the order specified by the combinations:

    > mat[combs[1,], ]
      A B C D
    w 0 0 1 1
    w 0 0 1 1
    w 0 0 1 1
    x 0 1 0 1
    x 0 1 0 1
    y 0 0 1 1
    > mat[combs[2,], ]
      A B C D
    x 0 1 0 1
    y 0 0 1 1
    z 0 0 0 1
    y 0 0 1 1
    z 0 0 0 1
    z 0 0 0 1
    

    To get exactly what the OP posted, we can modify the rownames using a second combn() call:

    > out <- mat[combs[1,], ] * mat[combs[2,], ]
    > rownames(out) <- apply(combn(rownames(dat), 2), 2, paste, collapse = "")
    > out
       A B C D
    wx 0 0 0 1
    wy 0 0 1 1
    wz 0 0 0 1
    xy 0 0 0 1
    xz 0 0 0 1
    yz 0 0 0 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given input file z b a f g a b ... I want to
My input file is going to be something like this key value key value
I have input file as 02.mp3. I want to change it to mp3 file
I need to scan an input file and search for a specific value. How
I have an input file that I want to sort based on timestamp which
Given an input file containing one single number per line, how could I get
My input file is as follows: 12/13/2011,07:14:13.724,12/13/2011 07:14:13.724,231.56.3.245,LasVegas,US I wish to get the following:
Given an input file of text lines, I want duplicate lines to be identified
I read the input file , which has 911 lines , and want to
Given a uncompressed input file with predefined frame format, need to build a simple

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.