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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:59:14+00:00 2026-06-17T13:59:14+00:00

I experience some trouble trying to create a function that contains a loop in

  • 0

I experience some trouble trying to create a function that contains a loop in R. The loop works fine but when I put it inside the body of my function it won’t write any Output any more.

So given my data with:

    df1<-data.frame(a=sample(1:50,10),b=sample(1:50,10),c=sample(1:50,10))

I create a vector to store my results and a loop with some function

    result <- vector("list",10)

    for (i in 1:10)
    {
      result[[i]] <- df1*i
    }

when I try to create a function like this

    # make the loop a function
    result2 <- vector("list",10)

    loop.function<-
      function(x,a,b){
        for (i in a:b)
      {
        result2[[i]] <- x*i
      }
    }
    loop.function(df1,1,10)

I get no data in “result2”. So I think there is some basic problem in the syntax. Can anyone help me out?

  • 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-17T13:59:15+00:00Added an answer on June 17, 2026 at 1:59 pm

    Your function does not return the list it created. You need to add a return command:

    result2 <- vector("list",10)
    
    loop.function<-
      function(x,a,b){
        for (i in a:b)
      {
        result2[[i]] <- x*i
      }
      return(result2) # added this row
    }
    

    By the way: A shorter version of this function could be created with lapply:

    myfun <- function(x, a = 1, b) {
        lapply(seq(a, b), "*", x)   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey having some trouble trying to maintain transparency on a png when i create
I had some experience of android but know almost nothing of GCC Makefile. Here
I have some experience using parallel extensions in .Net development, but I was looking
I have some experience with Java , C , databases , networking etc..But anything
Hi I have some experience with programming but I'm not very good with pointers.
Hope that someone can share their experience with some of the latest emerging backbone.js
trying to do some Android development, which means Eclipse, however, most of my experience
I have an application that's having some trouble handling multi-processor systems. It's not an
I am developing a small windows app, but have some trouble deciding whether to
I'm trying to do some URL rewriting, but my host (1and1) and my little

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.