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

The Archive Base Latest Questions

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

Is there a way I can make an lapply statement also show the index?

  • 0

Is there a way I can make an lapply statement also show the index? More specifically, consider the following example:

mylist <- list(c(5,4),c(3,2), c(1,3))

myfunction<- function(values){
  print("Adding values: ")
  return(values[1] + values[2])
}

lapply(mylist, myfunction)

Is there a way I can somehow make it print “Adding Values: 1” “Adding Values: 2” etc.. one element of each in the list?

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

    The function mapply works like lapply but allows you to supply multiple vectors or lists.

    In your case you can create a second vector, the same length of the list, just counting up:

    mapply(myfunction, mylist, seq_along(mylist))
    

    Let’s try it:

    myfunction<- function(values, index){ 
      cat("Adding values (", index, "): ", values[1], "...", values[2], " = ", sum(values), "\n" )
      invisible(values[1] + values[2])
    }
    
    mylist <- list(c(5, 4), c(3, 2), c(1, 3))
    
    mapply(myfunction, mylist, seq_along(mylist))
    

    Result:

    Adding values ( 1 ):  5 ... 4  =  9 
    Adding values ( 2 ):  3 ... 2  =  5 
    Adding values ( 3 ):  1 ... 3  =  4 
    

    Advanced user fun

    Just for fun, a careful reading of the ?lapply manual page reveals that the following also works:

    myfunction<- function(values){
        print(sprintf("Adding values: %i",substitute(values)[[3]]))
        return(values[1] + values[2])
    }
    
    lapply(mylist, myfunction)
    

    Which suggests a general function adaptor could be created to supply index to your original function (or any other), modified to expect a second index argument:

    myfunction<- function(values,index){
        print(sprintf("Adding values: %i",index))
        return(values[1] + values[2])
    }
    

    Now the adaptor

    lapply_index_adaptor=function(f)function(x,...)f(x,substitute(x)[[3]],...)
    

    and now the lapply call, with the adaptor:

    lapply(mylist, lapply_index_adaptor(myfunction))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to make find-name-dired to only show filenames that I can
is there any way i can make the following work, or is there a
Is there a way I can make joomla do the following: I have several
Is there any way I can make Firefox call a JavaScript function in my
Since there is no way that you can make the flash object transparent, there
Is there any way (utilizing Reflection I hope) that I can make an instantiated
Is there a way to make this method generic so I can return a
Is there some way I can make an Extension Method asynchronous? I have looked
Is there any way we can make the JW Player auto resizable when it
Is there some way I can make the matlab integrated editor not use emacs

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.