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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:32:14+00:00 2026-06-11T21:32:14+00:00

Thanks for all the help I got from just reading stuff. I’m not happy

  • 0

Thanks for all the help I got from just reading stuff.

I’m not happy with my R loops when I am only dealing within one data.frame because I have to write down the name of the dataframe over and over again which bloats up my R code.

Here is a silly example:

x<- rep(NA,10)
y <- 1:10
dat <- data.frame(x,y)

for(i in 2:nrow(dat)){
    dat$x[i] <- dat$y[i] + dat$y[i-1]
}

So what I want to get rid of is that dat$ -bit. Outside loops this can neatly be done with within(), but I am not exactly sure whether you can actually do that with R. I tried it though:

remove(x,y) # In order to avoid accidental usage of the initial vectors
within(dat,{
for(i in 2:nrow(dat)){
    x[i] <- y[i] + y[i-1]
}})

The output looks like this:

    x  y  i
1  NA  1 10
2   3  2 10
3   5  3 10
4   7  4 10
5   9  5 10
6  11  6 10
7  13  7 10
8  15  8 10
9  17  9 10
10 19 10 10

So the loop did actually work, it’s just that there is a new magical column.

Does anyone know (1) what is going on here and (2) how to elegantly deal with that kind of loops (a more complicated example wrapping within() around a loop including several if() statements and calculations failed btw?

Thanks a lot in advance!
skr

  • 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-11T21:32:16+00:00Added an answer on June 11, 2026 at 9:32 pm

    Ben answered your main question, by noting that i is being assigned to by the for loop. You can see that that is so by trying something like this:

    for(j in 1:3) cat("hi\n") 
    hi
    hi
    hi
    > j
    [1] 3
    

    One option is just to remove the unwanted i variable by making its value NULL:

    within(dat,{
    for(i in 2:nrow(dat)){
        x[i] <- y[i] + y[i-1]
    }
    i <- NULL
    })
    

    Another is to use with() instead of within():

    dat$x <- with(dat, {
        for(i in 2:nrow(dat)){
            x[i] <- y[i] + y[i-1]
        }
        x
    })
    

    Finally, though I realize yours was a toy example, the best solution will very often be to avoid for loops altogether:

    d <- data.frame(y=1:10)
    within(d, {x = y + c(NA, head(y, -1))})
    #     y  x
    # 1   1 NA
    # 2   2  3
    # 3   3  5
    # 4   4  7
    # 5   5  9
    # 6   6 11
    # 7   7 13
    # 8   8 15
    # 9   9 17
    # 10 10 19
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, thanks for all the help I've received so far from StackOverflow. I've learned
Firs of all thanks for reading this. I'm having trouble updating the progress from
Thanks for your help! I'd like to output all companyName entries that have uploads
I'm hoping someone can help me. I've got a specific Exception from COM that
Problem solved, thank you all for the help I've got a bit of a
Thanks to the help in a previous question , I've got the following code
How could this be happening? sqlite> select read, text from message; 1|No just got
First, I would to thank everyone for all the help they provide via this
Thanks to all that responded to my previous thread. There is still a problem
First of all thanks in advance, this has been very frustrating and I'm hoping

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.