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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:06:36+00:00 2026-05-26T09:06:36+00:00

Suppose I have two lists, and corresponding elements of the lists are the same

  • 0

Suppose I have two lists, and corresponding elements of the lists are the same shape:

 e1=list(1,c(1,2,3),matrix(1:12,3,4))
 e2=list(1,c(1,2,3),matrix(1:12,3,4))

and I want to add these two lists element-by-element. Here’s my solution which works for any length of lists and any shape of element, as long as they match and are addable:

> esum
function(e1,e2){
  e = list()
  for(i in 1:length(e1)){
    e[[i]]=e1[[i]]+e2[[i]]
  }
  e
}
> esum(e1,e2)

but it just seems ugly, and probably the kind of thing that can be done in a one-liner.

This is stage one of the problem, which is actually to add up a whole list of many of these lists, but once esum is defined its just Reduce:

 > ee = list(e1,e2,e1,e1,e2)
 > Reduce(esum,ee)[[3]]  # lets just check [[3]] for now
      [,1] [,2] [,3] [,4]
 [1,]    5   20   35   50
 [2,]   10   25   40   55
 [3,]   15   30   45   60

So, anyone got a one-liner for these?

Yes I know one-liners aren’t always the best things.

  • 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-26T09:06:37+00:00Added an answer on May 26, 2026 at 9:06 am

    Something like

       mapply("+",e1,e2)
    

    works for the first part …

    Reduce( function(x,y) mapply("+",x,y),ee)[[3]]
    

    There may be something even slicker. Reduce doesn’t take a ... argument so we can’t get away with Reduce(mapply,ee,FUN="+")[[3]]

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

Sidebar

Related Questions

Suppose I have two lists that holds the list of source file names and
Suppose I have a simple to-do list application. The application contains two models: lists
Suppose I have two lists, L and M. Now I want to know if
Suppose I have two lists of strings (list A and list B) with the
Suppose I have two lists: val a = List('a', 'b', 'c') val b =
Suppose I have two differently-sized lists a = [1, 2, 3] b = ['a',
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose I have two lists of numbers in files f1, f2, each number one
Suppose I have two lists of type MyCustomType , a class which could be
In Java, suppose I have a method that takes in two lists of integers,

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.