0

I have some values which i have calculated in different steps and i want to sum them iteratively:

elasped_time1<-10
elasped_time2<-20
elasped_time3<-40
elasped_time4<-30
elasped_time5<-20
elasped_time6<-10
elasped_time7<-20
elasped_time8<-10
elasped_time9<-30
elasped_time10<-20

Now i want to sum them iteratively by incrementing i in elasped_time[i]......

I tried this:

n = 10
for(i in 1:n){
 x[i]=elasped_time[i]
 i=i+1
 print(total_time<-sum(x))
 }
cat("Total time taken to run the codes (seconds):",total_time)

But failed miserably...help required!

Nishant
  • 1,063
  • 13
  • 40
  • 1
    Your `elapsed_time[i]` syntax is wrong; `elapsed_time1`, `elapsed_time2`, etc are not elements of a vector, but individual variables. A better approach would be not to store your initial values in individual variables, but into a vector, which is then easy to sum over. – adatum Jan 11 '17 at 04:26

1 Answers1

2

Iteratively named variables are terrible - they cause people to use difficult to write and more difficult to debug code using eval(parse(...)).

You seem to want a vector named elapsed_time with a bunch of values:

elapsed_time = c(10, 20, 40, 30, 20, 10, 20, 10, 30, 20)

Now you can access the ith element with elapsed_time[i]:

elapsed_time[2]
# [1] 20

and the cumulative sum is also easy:

cumsum(elapsed_time)
# [1]  10  30  70 100 120 130 150 160 190 210
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • i already had this @Gregor inmy edits coming – joel.wilson Jan 11 '17 at 05:08
  • If you edit your question to recommend addressing the problem at the root (never creating `elapsed_time1`, `elapsed_time2`, etc., in the first place) and add some language to strongly *discourage* the use of `eval(parse(...))` I'll happily delete my answer and upvote yours. – Gregor Thomas Jan 11 '17 at 05:10
  • i defdinitely agree with you, but could you help me understand why `eval()` should be avoided. Just for my understanding only – joel.wilson Jan 11 '17 at 05:12
  • 1
    There is [this question, What specifically are the danger of eval parse?](http://stackoverflow.com/q/13649979/903061), but it is being asked and answered from the position of someone who knows pretty well what they are doing. And there *are* good uses for it - but they are very advanced. What I think is really terrible is using (or struggling to use) `eval(parse())` due to ignorance of R basics - in this case OP didn't think to use a numeric vector, the *most basic* data type in R. Much more commonly people go to eval/parse because they are uncomfortable with lists. – Gregor Thomas Jan 11 '17 at 06:32
  • 1
    In these cases, where people try to use sequentially named variables, using they are already in a bad place because they are probably copy/pasting since their code is so repetitive - just look at OP's data definition. This is slow and error-prone. Then, compared to using a vector or list, eval parse is error-prone, difficult to debug, more difficult to read, and encourages more copy/pasting of code. Using lists is actually quite easy, and makes your code concise, readable, and generalizable. – Gregor Thomas Jan 11 '17 at 06:36
  • +1 for the reasons! I do agree as a beginner I should not have used eval() for this question. you have my thumbs up! – joel.wilson Jan 11 '17 at 08:08