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

  • Home
  • SEARCH
  • 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 8518237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:55:33+00:00 2026-06-11T05:55:33+00:00

(edit note: I changed the Title to R: enumerate column combinations of a matrix,

  • 0

(edit note: I changed the Title to “R: enumerate column combinations of a matrix”, from “R grep: matching a matrix of strings to a list” to better reflect the solution)

I am trying to match a matrix of strings to a list: so that i can ultimately use the matrix as a map in later operations on a data.frame.

This first part works as intended, returning a list of all the possible pairs, triples and quad combinations (though perhaps this approach has created my bind?):

priceList <- data.frame(aaa = rnorm(100, 100, 10), bbb = rnorm(100, 100, 10), 
            ccc = rnorm(100, 100, 10), ddd = rnorm(100, 100, 10), 
            eee = rnorm(100, 100, 10), fff = rnorm(100, 100, 10), 
            ggg = rnorm(100, 100, 10))

getTrades <- function(dd, Maxleg=3)
{
    nodes <- colnames(dd)
    tradeList <- list()
    for (i in 2:Maxleg){
        tradeLeg <- paste0('legs',i)
        tradeList[[tradeLeg]] <- combn(nodes, i)
    }
    return(tradeList)
}

tradeCombos <- getTrades(priceList, 4)

I’d now like to turn this list of possible combinations into trades. For example:

> tradeCombos[[1]][,1]
[1] "aaa" "bbb"

Needs to eventually become priceList[,2] - priceList[,1], and so forth.

I have tried a few approaches with grep and similar commands, and feel that i’ve come close with the following:

LocList <- sapply(tradeCombos[[1]], regexpr, colnames(priceList))

However the format is not quite suitable for the next step.

Ideally, LocList[1] would return something like: 1 2

Assuming that the tradeCombos[[1]][,1] == "aaa" "bbb".

Can someone please help?

__

With help from all of the answers below, i’ve now got:

colDiff <- function(x) 
{
    Reduce('-', rev(x))
}

getTrades <- function(dd, Maxleg=3)
{
    tradeList <- list()
    for (i in 2:Maxleg){
        tradeLeg <- paste0('legs',i)
        tradeLegsList <- combn(names(dd), i, 
            function(x) dd[x], simplify = FALSE)
        nameMtx <- combn(names(dd), i)
        names(tradeLegsList) <- apply(nameMtx, MARGIN=2, 
            FUN=function(x) paste(rev(x), collapse='*'))
        tradeList[[tradeLeg]] <- lapply(tradeLegsList, colDiff) 
    }
    return(tradeList)
}

tradeCombos <- getTrades(priceList, 4)

This retains the names of the constitutent parts, and is everything I was trying to achieve.

Many thanks to all for the help.

  • 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-11T05:55:35+00:00Added an answer on June 11, 2026 at 5:55 am

    This gets your eventual aim using lapply, apply, and Reduce.

    lapply(tradeCombos, 
     function(combos) 
     apply(combos, MARGIN=2, FUN=function(combo) Reduce('-', priceList[rev(combo)])))
    

    combo is a column from one of the combo matrices in tradeCombos. rev(combo) reverses the column so the last value is first. The R syntax for selecting a subset of columns from a data.frame is DF[col.names], so priceList[rev(combo)] is a subset of priceList with just the columns in combo, in reverse order. data.frames are actually just lists of columns, so any function that’s designed to iterate over lists can be used to iterate over the columns in a data.frame. Reduce is one such function. Reduce takes a function (in this case the subtract function -) and a list of arguments and then successively calls the function on the arguments in the list with the results of the previous call, e.g., (((arg1 – arg2) – arg3) – arg4).

    You rename the columns in tradeCombos so that the final column names reflect their source with:

    tradeCombos <- lapply(tradeCombos, 
        function(combos) {
            dimnames(combos)[[2]] <- apply(combos, 
                MARGIN=2, 
                FUN=function(combo) paste(rev(combo), collapse='-')
            )
            return(combos)
        }
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: The title of this was changed to better reflect the actual problem. I've
Edit note: After a seemingly enourmous amount of bad feedback MS got from their
EDIT:I've changed the title, because the issue had nothing to do with IE image.load()
Edit : Note that, as Daniel and latkin noted in an answer and a
EDIT I added this note to explain why I keep this question here. I
Note: The answer marked as the answer, answers the questions in the Title. However,
EDIT: changed the whole question sorry, im trying to deal with the mysql DATETIME
(EDIT: rewritten question to make it clearer, meaning hasn't changed) You can create an
Not sure if the title is correct, Please edit if you think of a
Edit Using the answers to the question I changed the test to the following

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.