1

Let's say that I have got two lists:

temp<-c("con.sin.results","sin.results","exp.results")
Temp<-c("[,1:16]","[,17:32]","[,33:48]","[,49:64]")

Each of the variables in temp contain 1000 observations and 64 variables. What i am trying to do is create a double loop so that I can create boxplots based on sample size (so that each boxplot would have 16 boxes, as per Temp) i.e I would get boxplot(con.sin.results[,1:16]), then boxplot(con.sin.results[,17:32]) etc.

With this goal in mind, I've gotten to the following point:

for (l in temp){
for (L in Temp){
windows()
par(mfrow=c(2,2))
A<-noquote(paste(noquote(l),noquote(L),sep=""))
boxplot(A)
}
}

Unfortunately, this spits out an error at me:

Error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator

Where am I going wrong? What should I adjust?

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Albort
  • 177
  • 1
  • 11
  • You cannot just stick strings together and expect them to execute, even with `noquote`. At the moment "temp" is a character vector with three elements. Please read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – IRTFM Oct 12 '11 at 06:07
  • No, i realise this. I guess it was more of a question in line with how you could get that string to execute. – Albort Oct 12 '11 at 22:41

2 Answers2

2

If I understand your question correctly, this is more or less what you want:

bplotforone<-function(mat, groups=list(1:16, 17:32, 33:48, 49:64), newwin=TRUE, mfrow=c(2,2))
{
    nr<-nrow(mat)
    if(newwin) windows()
    par(mfrow=mfrow)
    for(curgroup in groups)
    {
        newres<-as.vector(mat[,curgroup])
        newres<-data.frame(vals=newres, grp=rep(seq_along(curgroup), each=nr))
        boxplot(vals~grp, data=newres)
    }
}

con.sin.results2<-matrix(runif(10*64), ncol=64) #generated some test data here
bplotforone(con.sin.results2)

You can now easily do something like:

listOfResults<-list(con.sin.results,sin.results,exp.results) #note: no quotes!!
for(curres in listOfResults) bplotforone(curres)
Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57
  • Nice. Like it alot. My supervisor came up with a different approach. Ah the joy of programming. – Albort Oct 12 '11 at 22:47
1

This is what my supervisor came up with:

temp<-c("con.sin.results","sin.results","exp.results")
N<-c(50,100,250,500)
con.sin.results<-matrix(runif(100*64),100,64)
sin.results<-matrix(runif(100*64),100,64)
exp.results<-matrix(runif(100*64),100,64)

for (I in temp){
  windows()
  par(mfrow=c(2,2))
  for (n in N){
    if (n==50) eval(parse(text=paste("boxplot(",I,"[,1:16])",sep=""))) 
    if (n==100) eval(parse(text=paste("boxplot(",I,"[,17:32])",sep=""))) 
    if (n==250) eval(parse(text=paste("boxplot(",I,"[,33:48])",sep=""))) 
    if (n==500) eval(parse(text=paste("boxplot(",I,"[,49:64])",sep="")))
  }
}
Albort
  • 177
  • 1
  • 11