[R] Removing leading and trailing spaces (string manipulation)

Marc Schwartz MSchwartz at MedAnalytics.com
Wed Mar 31 16:53:38 CEST 2004


On Wed, 2004-03-31 at 08:44, dsheuman at rogers.com wrote:
> Hi all,
> 
> I'm running the following code to generate 40 different jpegs based on
> the resulting data.  I'd like the file names to be 'Cluster1.jpeg',
> however the code write filenames like 'Cluster 1 .jpeg'.
> 
> How can I get rid of the unwanted spaces?  I've looked at ?format and
> it doesn't seem to work - at least in this context.
> 
> 
> ###################
> ClusCount <- 40
> 
> datain <- as.data.frame( read.csv("c:\\daclus.csv"))
> 
> for(i in 1:ClusCount){
> 	mapit <- subset(datain, cluster == i)
> 	jpeg(file=paste("c:\\temp\\cluster",format(i),".jpeg"), width = 640,
> height = 480, pointsize = 12,quality = 300, bg = "white")
> 	plot( as.matrix(mapit[,2]),
> as.matrix(mapit[,3]),xlim=c(-141.6,-52.1),ylim=c(41.5,83.7),  type =
> "p", main = paste("Cluster",i) )
> 	dev.off()
> }
> ###################
> 
> 
> Thanks,
> 
> Danny


Change the code in your 'jpeg' statement to read:

jpeg(file=paste("c:\\temp\\cluster",format(i),".jpeg", sep = ""), ...
                                                       ^^^^^^^^

You can use the 'sep' argument (which defaults to " ") to eliminate the
spaces between the pasted components.

See ?paste for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list