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.

Topdombili
  • 265
  • 4
  • 10
  • 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. – BlueTrin Jan 07 '13 at 11:10
  • @BlueTrin, What do u mean by code? I put my code in the question. And corresponding error is updated in question. – Topdombili Jan 07 '13 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 – ECII Jan 07 '13 at 11:17
  • 1
    @Topdombili or just add str(gsbi1_30) and a sentence explaining what you want to plot?which variables? – agstudy Jan 07 '13 at 11:28
  • Sorry for confusion, ECII is right, that is what I meant. – BlueTrin Jan 07 '13 at 12:56
  • Thanks for editing the post, but your sample data doesn't tell us what we need to know -- it's not the numeric values, but the data structures, that are uncertain. Please use `dput` or `str`, and see http://tinyurl.com/reproducible-000 ... – Ben Bolker Jan 07 '13 at 14:21
  • Thanks for your support. I think they are numeric: str(ob_30) num [1:24] -0.6 -1.2 -1 -0.8 -0.4 -0.2 0.1 0.1 0.7 1 ... – Topdombili Jan 07 '13 at 14:58

1 Answers1

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")
})
Theodore Lytras
  • 3,955
  • 1
  • 18
  • 25
  • 1
    +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): http://stackoverflow.com/questions/2275896/is-rs-apply-family-more-than-syntactic-sugar/2276001#2276001 Certainly for this case (creating four plots) I can't see it making a difference – Ben Bolker Jan 07 '13 at 13:54
  • 1
    you could write this as `lapply(lapply(temp,"[[","ambtemp"),plot, type="o", pch=22, lty=22, ...)` – Ben Bolker Jan 07 '13 at 13:58
  • @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? – Theodore Lytras Jan 07 '13 at 14:26
  • 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 ... – Ben Bolker Jan 07 '13 at 14:36
  • I think my feelings are confirmed by the fact that the OP is now working on another question http://stackoverflow.com/questions/14203567/syntax-error-in-ggplot-function-in-r where they're confused by the syntax of `lapply`. If they had stuck with `for` loops they'd be better off for now ... – Ben Bolker Jan 07 '13 at 20:53