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!