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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:35:22+00:00 2026-06-11T06:35:22+00:00

I have a list of data.frames, and would like to operate on their columns,

  • 0

I have a list of data.frames, and would like to operate on their columns, using various weights.

For example, subtracting the first columns from the second column (solved, see below); or subtracting the first and third from twice the second (unsolved).

Thanks to the generous help obtained in response to this question, I have a solution to the the problem in two dimensions without weights using Reduce.

I would like to have the flexibility to operate with weights – and in higher dimesions.

What I have so far is:

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

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

tradeLegsList <- combn(names(priceList), 3, function(x) priceList[x], simplify = FALSE)

tradeList <- lapply(tradeLegsList, colDiff)

From what I can tell, Reduce is not designed to take multiple arguments.

I can do this the long way with 2* tradeLegsList[[1]]$bbb - tradeLegsList[[1]]$aaa - tradeLegsList[[1]]$ccc, and some loops, but it doesn’t seem like the R way.

Is there a way to pass in a weight vector?

Ideally, I would to pass an argument such as w = c(-1, 2, -1) to the colDiff (or Reduce) function … or something similar.

  • 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-11T06:35:24+00:00Added an answer on June 11, 2026 at 6:35 am

    True, Reduce is not geared to allow multiple arguments, just two for each reduction. Therefore it is easiest to premultiply the elements in the list you are Reduce-ing.

    Below is a solution that does this using mapply within your colDiff function definition.

    Change your definifion of colDiff to allow a weight vector, and apply this using mapply
    with SIMPLIFY = F.

    EDIT

    In light of the comments, weighting depends on the number of columns and there being no need for the rev

    The weighting by length

    length(x) == 1 -> w = 1
    length(x) == 2 -> w = c(-1, 1),
    length(x) == 3 -> w = c(-1, 2, -1),
    length(x) == 4 -> w = c(-1, 1, -1, +1)

    weighting <- function(i){
      switch(i, 1, c(-1,1), c(-1,2,-1), c(-1,1,-1, 1))
    }
    colDiff <- function(x) 
        {
            w = weighting(length(x))
            Reduce('+', mapply('*', x, e2 = w, SIMPLIFY = F))
        }
    

    Then something like this would work

    tradeList <- lapply(tradeLegsList, colDiff)
    

    you could also keep with the functional programming theme and use Map which is a simple wrapper for mapply with SIMPLIFY = F

    colDiff <- function(x) 
            {
                w = weighting(length(x))
                Reduce('+', Map('*', x , e2 = w))
            }
    

    you could also prefine the weighting within the function colDiff (which may be easier).
    weighting[[2]] is weighting for when there are 2 columns, weighting[[3]] when there are 3.

    colDiff <- function(x) 
            {
             weighting <- list(1, c(-1,1), c(-1,2,-1), c(-1,1,-1, 1))             
                w = weighting[[length(x)]]
                Reduce('+', Map('*', x , e2 = w))
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list made up of several data frames. I would like to
I have a list L of data frames with two columns each...a key, and
I have a number of operations on data frames which I would like to
I have a list of data frames, all containing numeric data. How would I
I have a whole bunch of data.frames with irregular time spacing. I would like
I have big data frame with various numbers of columns and rows. I would
I have a log list of data with the first two columns as potential
I have a list within a data frame (in this example, df$candpct ), and
I have a list of data in javascript that looks like this: [[152, 48,
I have an R data frame that looks like this: z = as.data.frame(list(Col1=c(a,c,e,g),Col2=c(b,d,f,h),Col3=c(1,2,5,3,5,7,9,8,1))) >

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.