Hi R users I like to create a ASCII type file using cat() and paste() x <- round(runif(30),3) cat("vector =( ", paste(x,sep=""), " )\n", file = "vector.dat",sep=",") when I open vector.dat it was a long ugly file vector =( ,0.463,0.515,0.202,0.232,0.852,0.367,0.432,0.74,0.413,0.022,0.302,0.114,0.583,0.002,0.919,0.066,0.829,0.405,0.363,0.665,0.109,0.38,0.187,0.322,0.582,0.011,0.586,0.112,0.873,0.671, ) Also there was some problems right after opening parenthesis and before the closing parenthesis. Two comma were there I like to to have a nice formatted one like below. That is, 5 random values per a line vector =( 0.463,0.515,0.202,0.232,0.852, 0.367,0.432,0.74,0.413,0.022, 0.302,0.114,0.583,0.002,0.919, 0.066,0.829,0.405,0.363,0.665, 0.109,0.38,0.187,0.322,0.582, 0.011,0.586,0.112,0.873,0.671) I would be appreciative if I get some help TM
Adaikalavan Ramasamy
2006-Feb-09 12:45 UTC
[R] writing a file using both cat() and paste()
With regards to the saving bit, you might want to try dput() or save() as well. On Thu, 2006-02-09 at 19:29 -0500, Jim Lemon wrote:> Taka Matzmoto wrote: > > Hi R users > > > > I like to create a ASCII type file using cat() and paste() > > > > x <- round(runif(30),3) > > cat("vector =( ", paste(x,sep=""), " )\n", file = "vector.dat",sep=",") > > > > when I open vector.dat it was a long ugly file > > > > vector =( > > ,0.463,0.515,0.202,0.232,0.852,0.367,0.432,0.74,0.413,0.022,0.302,0.114,0.583,0.002,0.919,0.066,0.829,0.405,0.363,0.665,0.109,0.38,0.187,0.322,0.582,0.011,0.586,0.112,0.873,0.671, > > ) > > > > Also there was some problems right after opening parenthesis and before the > > closing parenthesis. Two comma were there > > > > I like to to have a nice formatted one like below. That is, 5 random values > > per a line > > > > vector =( 0.463,0.515,0.202,0.232,0.852, > > 0.367,0.432,0.74,0.413,0.022, > > 0.302,0.114,0.583,0.002,0.919, > > 0.066,0.829,0.405,0.363,0.665, > > 0.109,0.38,0.187,0.322,0.582, > > 0.011,0.586,0.112,0.873,0.671) > > > First, you might want to avoid using "vector", as that is the name of an > R function. Say you have a 30 element data vector as above. If you > wanted to write a fairly general function to do this, here is a start: > > vector2file<-function(x,file="",values.per.line=5) { > if(nchar(file)) sink(file) > cat(deparse(substitute(x)),"<-c(\n") > xlen<-length(x) > for(i in 1:xlen) { > cat(x[i]) > if(i<xlen) cat(",") > if(i%%values.per.line == 0) cat("\n") > } > cat(")") > if(i%%values.per.line) cat("\n") > if(nchar(file))sink() > } > > Jim > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Dear group, I am a novice programmer in R. I have a list that has a length of 27 elements. Each element is derived from table function.>lls <- table(drres)>legnth(lls)27 I want to plot all these elements in 9x3 plot (9 rows and 3 columns) par(9,3)> mypltfunc <- function(mydata){+ for (i in 1:27){ + plot(unlist(mydata[i])) + } + }> mypltfunc(lls) >In the graphics window, all 27 figures are drawn in fraction of sec, one by one and I get to see the last graph. It is not drawing into this 9X3 grid. Could any one help me please. Thanks sri
Try par( mfrow=c(9,3) ) for(i in 1:27) plot( lls[[i] ) but I think it might be a little crowded to put 9 rows in a page. Also check out the lattice package which is bit more complicated to learn but gives prettier output. Regards, Adai On Thu, 2006-02-09 at 11:52 -0800, Srinivas Iyyer wrote:> Dear group, > I am a novice programmer in R. I have a list that > has a length of 27 elements. Each element is derived > from table function. > > >lls <- table(drres) > > >legnth(lls) > 27 > > I want to plot all these elements in 9x3 plot (9 rows > and 3 columns) > par(9,3) > > mypltfunc <- function(mydata){ > + for (i in 1:27){ > + plot(unlist(mydata[i])) > + } > + } > > > mypltfunc(lls) > > > > In the graphics window, all 27 figures are drawn in > fraction of sec, one by one and I get to see the last > graph. It is not drawing into this 9X3 grid. > > Could any one help me please. > > Thanks > sri > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
This works : # simulate some data mylist <- list(NULL) for(i in 1:27) mylist[[i]] <- rnorm( rpois( 1, lambda=20 ) ) # execute par( mfrow=c(9,3) ) par(mar = c(1,1,1,1), oma = c(1,1,1,1)) for(i in 1:27) plot( mylist[[i]] ) Also if you just want to plot the distribution values etc, then you can also try different possibilities such as boxplot( mylist ) Regards, Adai On Thu, 2006-02-09 at 14:05 -0800, Srinivas Iyyer wrote:> hi sarah, > thanks for your mail. > > ################################################# > > par(mfrow=c(9,3)) > > mypltfunc(lls) > Error in plot.new() : figure margins too large > > par(mfcol=c(9, 3)) > > mypltfunc(lls) > Error in plot.new() : figure margins too large > > ################################################## > > unfortunately I had this problem before. Thats the > reason, I went on using more simply, par(9,3). > > I tried the following too, although, truely I did not > understand the much after doing ?par: > > > mar = c(1,1,1,1) > > oma = c(1,1,1,1) > > par(mar,oma) > [[1]] > NULL > > [[2]] > NULL > > > mypltfunc(lls) > > > > By doing this the problem turned out that it printed > all 27 figures, one after other in fraction of second, > and I see the last figure. > > > > given my background (molecular biology) sometimes it > is very very difficult to understand the documentation > due to terminology problem. > > thanks > sri > > > > --- Sarah Goslee <sarah.goslee at gmail.com> wrote: > > > > > > > I want to plot all these elements in 9x3 plot (9 > > rows > > > and 3 columns) > > > par(9,3) > > > > > > You need to specify what par you want - see ?par for > > details. > > In this case, either > > > > par(mfrow=c(9,3)) > > or > > par(mfcol=c(9, 3)) > > > > will do what you want. > > > > Sarah > > -- > > Sarah Goslee > > http://www.stringpage.com > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Taka Matzmoto wrote:> Hi R users > > I like to create a ASCII type file using cat() and paste() > > x <- round(runif(30),3) > cat("vector =( ", paste(x,sep=""), " )\n", file = "vector.dat",sep=",") > > when I open vector.dat it was a long ugly file > > vector =( > ,0.463,0.515,0.202,0.232,0.852,0.367,0.432,0.74,0.413,0.022,0.302,0.114,0.583,0.002,0.919,0.066,0.829,0.405,0.363,0.665,0.109,0.38,0.187,0.322,0.582,0.011,0.586,0.112,0.873,0.671, > ) > > Also there was some problems right after opening parenthesis and before the > closing parenthesis. Two comma were there > > I like to to have a nice formatted one like below. That is, 5 random values > per a line > > vector =( 0.463,0.515,0.202,0.232,0.852, > 0.367,0.432,0.74,0.413,0.022, > 0.302,0.114,0.583,0.002,0.919, > 0.066,0.829,0.405,0.363,0.665, > 0.109,0.38,0.187,0.322,0.582, > 0.011,0.586,0.112,0.873,0.671) >First, you might want to avoid using "vector", as that is the name of an R function. Say you have a 30 element data vector as above. If you wanted to write a fairly general function to do this, here is a start: vector2file<-function(x,file="",values.per.line=5) { if(nchar(file)) sink(file) cat(deparse(substitute(x)),"<-c(\n") xlen<-length(x) for(i in 1:xlen) { cat(x[i]) if(i<xlen) cat(",") if(i%%values.per.line == 0) cat("\n") } cat(")") if(i%%values.per.line) cat("\n") if(nchar(file))sink() } Jim