0

I am not understanding some mechanism of how 'repeat' works and in dire need of help.

Given two initial variables:

i <- 0
my.sum <- i

How do I successfully create a vector that increases i by 1, computes i^2, adds this to 'my.sum' and prints a vector reporting 'i' and 'my.sum' for each iteration?? I want to terminate this loop before 'my.sum' (the sum of squared i's) exceeds 250.

Here is my most recent attempt:

i <- 0
my.sum <- i

  repeat { 
    sums <- c()
    my.sum.1 <- i+1
    my.sum.2 <- i^2
    my.sum <- c((my.sum + my.sum.1),(my.sum + my.sum.2))
    if (my.sum > 249){
      break
    }
     print(my.sum)
  }

which returns the warning:

the condition has length > 1 and only the first element will be used

and the error:

Error: vector memory exhausted (limit reached?)

I've had trouble with the first error in the past, most likely because I'm not sure exactly how vectors in for loops and 'repeat' work. Is this the source of my error or does it go deeper?

I do enjoy working problems on my own, but I often find I get stumped by fundamental concepts. What can I do to make this code work and how can I do better in the future?

Any help is greatly appreciated, thanks!

and98
  • 11
  • 2
  • 1
    Doesn't appear to be a problem with `repeat`. You are not understanding how `if` works. You might get success if you tested the second element of my.sum for being LT 250. And what you are calling the first error was called a Warning, NOT an error. It's the warning that gets printed out when if is given a vector of length greater than 1. – IRTFM Feb 07 '20 at 02:37
  • Sounds like you need a `while` loop rather than `repeat`. Maybe read through this first & come back if you get stuck? https://www.datacamp.com/community/tutorials/tutorial-on-loops-in-r?utm_source=adwords_ppc&utm_campaignid=1655852085&utm_adgroupid=61045433942&utm_device=c&utm_keyword=%2Bloops%20%2Br&utm_matchtype=b&utm_network=g&utm_adpostion=1t1&utm_creative=318880582254&utm_targetid=aud-764068346625:kwd-589281898934&utm_loc_interest_ms=&utm_loc_physical_ms=1011067&gclid=CjwKCAiAj-_xBRBjEiwAmRbqYl355SvsZbgI2ThRYEOF3uJBh5Lgd9nO3MktQTpCRqh2dHipbnhlBxoCQIgQAvD_BwE – Adam B. Feb 07 '20 at 02:44
  • 1
    @AdamB. Another user also suggested this function, I haven't learned about it yet but I'm definitely going to look into it, thank you for the suggestion! – and98 Feb 07 '20 at 02:57

2 Answers2

2

I would do it in a much simpler way using while

i <- 0 
my.sum <- i

while (my.sum <= 250) {

  i <- i + 1
  my.sum <- my.sum + i ^ 2

  cat("The value of i is:", i, "and the squared sum is", my.sum, "\n")  
}

Is this something similar to what you are looking?

Tomas Capretto
  • 721
  • 5
  • 6
  • yes! We cover new functions/types of problems weekly given lecture notes and online reading, I don't think we've made it to this topic yet but it sure would've been useful! Thanks for your help, I'm going to look into it some more – and98 Feb 07 '20 at 02:56
1

See my comment for where I think you were getting confused:

> repeat { 
+     sums <- c()
+     i <- i+1
+     my.sum <- i^2 +my.sum
+     
+     if (my.sum > 249){
+         break
+     }
+    print ( paste(i, my.sum ))
+ }
[1] "1 1"
[1] "2 5"
[1] "3 14"
[1] "4 30"
[1] "5 55"
[1] "6 91"
[1] "7 140"
[1] "8 204"
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I edited my question in response to your comment, thank you for that clarification and for your help! I'm going to continue reading up on the topic and try some more practice. – and98 Feb 07 '20 at 02:54
  • I think I answered your question fully. – IRTFM Feb 07 '20 at 05:03