[R] Using the 'by' function withing a 'for' loop

From: Judith Flores <juryef_at_yahoo.com>
Date: Mon, 21 Apr 2008 10:40:42 -0700 (PDT)

Dear R experts,

    I am trying to optimize my script, because right now it requires a lot of memory. The goal is to generate four plots in one page. Every plot corresponds to the means and sem's calculated for a given variable at different days. In order to obtain the means and sem's I apply the 'by' function. The way I have done it so far is like this:

Read the data
Generate a summary of the mean and sem of a variable at every Day.
Plot the mean and sem of that variable.

Repeat the same process for the other 3 variables.

  I tried to optimize the code by using a for loop, the code is below.   

#Reading the data

names(dato)<-c("id","day","tx","var1","var2","var3","var4") dato<-dato[,1:7]

#Specify varible to be plotted


#Define parameters of window where panel: margins,
number of plots in the panel
windows(height=9, width=9, rescale='fixed') par(mfrow=c(2,2),xpd=T, bty='l',
omi=c(0.8,0.25,1.2,0.15), mai=c(1.1,0.8,0.3,0.3))

for (k in variable) {     


    summ<-by(dat,dat[,c("tx","day")], function(x) {



    #Definining x axis range
    xmax<-unique(max(summ$day,na.rm=TRUE))     xmin<-unique(min(summ$day,na.rm=TRUE))     


plot(1,1,type='n',xlab='Day',xlim=c(xmin,xmax),ylim=c(yaxmin,yaxmax), ylab=k,        




    Where variable is a vector that specifies all the variables I want to plot.

But I am getting the following error:

“Error in var(as.vector(x), na.rm = na.rm) : 'x' is empty
In addition: Warning message:
In mean.default(x$k) : argument is not numeric or logical: returning NA”

   Could some one please show me how to structure my code to achieve my final goal, which is to simplify it?

I am attaching a csv file in case you want to run my code.

Thank you very much in advance for your time and help,


Be a better friend, newshound, and

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 21 Apr 2008 - 18:11:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Apr 2008 - 19:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive