[R] how to loop through names ?

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Mon Mar 22 18:12:58 CET 2004


Try plot( sales[ 13:24, "serial" ], sales[ 13:24, i ], xlab="Month No",
ylab="No/month")

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Pete Phillips
> Sent: 06 March 2004 09:53
> To: r-help at stat.math.ethz.ch
> Subject: [R] how to loop through names ?
>
>
>
> Hi
>
> I'm sure I'm missing something very straighforward here  :-(
>
> I have a data set 'sales' as follows:
> ==========================================
> # read in the sales data
> sales<-read.table("sales.dat",header=TRUE);
> #generate a serial field
> sales$serial=c(1:24)
>
> sales
>
>    an  l ml  ne ni total serial
> 1  43 25 35  51 17 69    1
> 2  38 18 47  94 3  99    2
> .......
> 24 58 13 41  95  4 1     24
> ===========================================
>
> (extra rows and columns deleted).
>
> I wish to produce a postscript plot file for each column plotted vs the
> serial column, using either the 1st to 12th row or the 13th to 24th
> rows, where the filename consists of the column name with '.ps' added.
>
> This the code I have so far:
>
> ============================================
> for (i in 1:(length(names(sales))-1)) {
>
> fname <- paste(names(sales)[[i]],".ps",sep="")
>
> postscript(file=fname)
>
> plot(
> sales$serial[13:24],
> sales[names(sales)[[i]]][13:24],
> xlab="Month No", ylab="No/month")
>  dev.off()
>
> }
> ==============================================
>
> The filename generation works (yay!), but I think I have missed
> something very basic here as that plot line seems too complex (and
> doesn't work!).
>
> Any ideas please ?
>
> Pete
> --
> Pete Phillips, Deputy Director,     |   http://www.smtl.co.uk/
> Surgical Materials Testing Lab,     |   http://www.worldwidewounds.com/
> Princess of Wales Hospital, S Wales |   http://www.dressings.org/
> Tel/Fax: +44 1656-752820/30         |   pete at smtl.co.uk
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list