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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:46:10+00:00 2026-05-31T16:46:10+00:00

I have a loop that works but not when I try and use it

  • 0

I have a loop that works but not when I try and use it in a function.
The below shows the data frame before and after the loops

DF

  MSU
1  12
2  11
3   6
4   5
5   6
6   6

Loop

for (i in 1:nrow(DF)) 
{  
    if(i<4) { DF[ i, 2 ] <- 0} else 
    {
        DF[ i, 2 ] <- ((DF[1,1] + DF[i-1,1] + DF[i-2,1] + DF[i-3,1])/4) 
    }
}

DF after loop, an extra column has been added with the average of the last four values unless the number of values was less than four.

  MSU    V2
1  12  0.00
2  11  0.00
3   6  0.00
4   5 10.25
5   6  8.50
6   6  7.25

Function with Loop

myfct <- function(DF){
for (i in 1:nrow(DF)) 
{  
    if(i<4) { DF[ i, 3 ] <- 0} else 
    {
        DF[ i, 3 ] <- ((DF[i,1] + DF[i-1,1] + DF[i-2,1] + DF[i-3,1])/4) 
    }
}
}

DF after function with Loop, there should ba an extra column with the same values as column 2 but it is not added. I have used print to show that the loop is being executed and the values are what I expect to put in the 3rd column.

  MSU    V2
1  12  0.00
2  11  0.00
3   6  0.00
4   5 10.25
5   6  8.50
6   6  7.25
  • 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-31T16:46:11+00:00Added an answer on May 31, 2026 at 4:46 pm

    R passes arguments by value, not by reference. This means that when you pass an object into a function, R creates a local copy of the object. Any changes you make to the object inside the function affect only the local copy, and not the original.

    To modify DF you need to make your function return the newly modified data frame, and overwrite the old value of DF with it.

    To do this you add the line return(DF) at the end of your function, and then call it with

    DF <- myfct(DF)
    

    which results in

    > DF
      MSU    V2   V3
    1  12  0.00 0.00
    2  11  0.00 0.00
    3   6  0.00 0.00
    4   5 10.25 8.50
    5   6  8.50 7.00
    6   6  7.25 5.75
    

    The reason that the columns V2 and V3 are not equal is that there’s a bug in your original loop code: you have written DF[1,1] where you presumably intended to write DF[i,1].

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

Sidebar

Related Questions

I have a complex function (optimisation) that can potentially enter in a loop or
I have a do...while loop and a try...catch block that wraps around database centric
I have a loop that reads each line in a file using getline() :
I have a loop that finds duplicate lines in a .ini file. I can
I have a loop that runs through a variety of websites and I'd like
I have a loop that runs for approx. 25 minutes i.e 1500 seconds. [100
I have a loop that calculates the similarity between two documents. It collects all
I have a loop that goes through all the news items we have on
Currently I have a loop that tries to find an unused filename by adding
In my WPF client, I have a loop that calls a WCF service to

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.