2

I wanted to create a class in R. I have number of tables and I wanted to plot them by a function. The code I have used is:

temp <- data.frame(gsbi1_30,gsbi1_29,ob_30,ob_29)

where gsbi1_30,gsbi1_29,ob_30,ob_29 are tables.

par(mfrow=c(2,2))
for (i in temp){ plot(i$ambtemp,type="o", pch=22, lty=2, col="brown",xlab = "Hour  2007/09/29" , ylab= "Ambient Tempreture" )
                 title(main="Hourly Mean, node 25", col.main="brown", font.main=1) }

And I came up with this error:

Error in plot(i$ambtemp, type = "o", pch = 22, lty = 2, col = "brown",  : 
  error in evaluating the argument 'x' in selecting a method for function 'plot': Error in i$ambtemp : $ operator is invalid for atomic vectors

Sample Data:

-0.6 -1.2 -1.0 -0.8 -0.4 -0.2

All the other samples are in the same structure.

7
  • 1
    Can you add some code to generate some dummy values for the variables gsbi and ob? This will help other people to help you. Commented Jan 7, 2013 at 11:10
  • @BlueTrin, What do u mean by code? I put my code in the question. And corresponding error is updated in question. Commented Jan 7, 2013 at 11:12
  • 2
    @Topdombili I think what BlueTrin means is to give us some data for gsbi1_30,gsbi1_29,ob_30,ob_29 Commented Jan 7, 2013 at 11:17
  • 1
    @Topdombili or just add str(gsbi1_30) and a sentence explaining what you want to plot?which variables? Commented Jan 7, 2013 at 11:28
  • Sorry for confusion, ECII is right, that is what I meant. Commented Jan 7, 2013 at 12:56

1 Answer 1

1

The problem is that you shouldn't create temp as a data.frame in the first place. If gsbi1_30, gsbi1_29, ob_30 and ob_29 are themselves data.frames (as I suspect), data.frame() will combine their columns to produce a big data.frame.

Instead, create a list:

temp <- list(gsbi1_30,gsbi1_29,ob_30,ob_29)

and iterate over it with lapply() (for loops are very inefficient in R):

par(mfrow=c(2,2))
lapply(temp, function(i) {
    plot(i$ambtemp, type = "o", pch = 22, lty = 2, col = "brown", xlab = "Hour  2007/09/29" , ylab = "Ambient Tempreture")
})
Sign up to request clarification or add additional context in comments.

5 Comments

+1 for a good guess about what's going wrong (we'll see if the OP confirms that). You're wrong about "for loops are very inefficient in R", however (at least it's more subtle than that): stackoverflow.com/questions/2275896/… Certainly for this case (creating four plots) I can't see it making a difference
you could write this as lapply(lapply(temp,"[[","ambtemp"),plot, type="o", pch=22, lty=22, ...)
@BenBolker Many thanks for setting this straight. And certainly, creating a couple of plots is not a performance-critical task. In any event, I believe using the apply family helps people "vectorize" their thinking, don't you aggree?
I agree -- in the long run it makes things nicer to apply. In the short term I try to be careful about adding to people's cognitive load while they're in the middle of trying to solve a problem. I'd rather read code with apply than for, but if the for loop works just fine for a given problem ...
I think my feelings are confirmed by the fact that the OP is now working on another question stackoverflow.com/questions/14203567/… where they're confused by the syntax of lapply. If they had stuck with for loops they'd be better off for now ...

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.