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
On Sat, 06 Mar 2004 09:52:31 +0000, "Pete Phillips" <pete at smtl.co.uk> wrote :>sales<-read.table("sales.dat",header=TRUE);...>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!).I think your indexing is off a bit. "sales" is a data frame, so something like sales[13:24, "an"] would select some rows from the column named "an". Thus sales[13:24, names(sales)[i]] would work, or even more simply sales[13:24, i]. You don't often need the double bracket indexing "[[i]]". It is used with lists, to select the element; names(sales) is a vector, not a list, so it only needs single brackets. Duncan Murdoch
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 >
Try this simplification which loops through the names rather than their index and improves plot a bit too: ix <- 13:24 for( n in names(sales) ) { fname <- paste( n, "ps", sep="." ) postscript( file = fname ) plot( sales[ix,"serial"], sales[ix,n], xlab = "Month No", ylab = "No/month", main = n ) dev.off() } or use the subset= argument to plot.formula: plot( sales[,n] ~ sales$serial, subset = ix, xlab = "Month No", ylab = "No/month", main = n ) Date: Sat, 06 Mar 2004 09:52:31 +0000 From: Pete Phillips <pete at smtl.co.uk> 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