0

I am new to r and hope to get some help with using a loop function to create graphs.

I hope to create 288 graphs, and the data are in stacked format. Each graph is created from 30 rows of data, so in total I have 288*30 = 8640 rows in my data.

I managed to create a first graph using this code-

 # setting range and table
 xrange <- range(0,300)
  yrange <- range(1,15)
  plot(xrange, yrange, type="n", xlab="Time in seconds", ylab="Performance")

# adding lines
lines(df$TimeStamp0[1:30],df$Pur[1:30], type="o", pch=4, col="red")
lines(df$TimeStamp0[1:30],df$Yel[1:30], type="o", pch=4, col="blue")
lines(df$PartTimeStamp0[1:30],df$PartPur[1:30], type="o", pch=20, col="green")
lines(df$PartTimeStamp0[1:30],df$PartYel[1:30], type="o", pch=20, col="orange")

There are four lines for four different performance components. I hope to create a loop to get this syntax to generate a graph every 30 rows through my entire dataset. I tried the "for loop" command (see below) but couldn't get it to work.

for(i in 1:288) {
   startRow=1, endRow=startRow+29
   #pasted the above graph creation syntax
   }

I would really appreciate any help, thanks!!

4
  • Are you just copying the same lines in the loop? You should change the various [1:30] to [startRow:endRow] Commented Feb 20, 2014 at 20:20
  • 4
    whatcha gonna do with 288 graphs? Commented Feb 20, 2014 at 20:21
  • your loop runs from 1:288 generating the variable 'i' with a given number, but I don't see i anywhere in your loop code. I also don't see you updating your startRow each loop. You really need to do one of the two. Right now that loop just generates i 288 times, does nothing with it, and makes the same graph over and over. Commented Feb 20, 2014 at 21:38
  • yes, you are right, I just changed it to for(i in seq(1, 8640, by=30) and also my [1:30] to [startRow:endRow], and everything works now. thanks a lot!! Commented Feb 20, 2014 at 22:14

1 Answer 1

2

Since you want a graph every 30 lines, you don't want to use the range 1:288; instead, you should use:

for(i in seq(1, 8640, by=30){

Also, you need to change the "1" in "startRow=1" to "i", i.e.

startRow=i
endRow=startRow+29
Sign up to request clarification or add additional context in comments.

1 Comment

yes you are right about the error in my loop code. everything is working now. THANKS SO MUCH!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.