Hi, R experts: I am trying to generate data output in the following format: rom_array := ( "00000000000000000000000000000000", "00000000010010010110111111101100", "00000000100100101101000001011111", "00000000110111000001000111100011", "00000001001001010010010100001001", "00000001011011011111101001101001") I have all the necessary data line, however, I am having trouble generating the double quotation marks along with the trailing comma for each line. Anyone can help? Thanks. [[alternative HTML version deleted]]
> t <- c(+ "00000000000000000000000000000000", + "00000000010010010110111111101100", + "00000000100100101101000001011111", + "00000000110111000001000111100011", + "00000001001001010010010100001001", + "00000001011011011111101001101001")> {+ cat ('rom_array := (\n'); + for (i in 1:length(t)) { + cat('"', t[i], '"', + ifelse(i == length(t), '', ',\n'), sep='') + }; + cat(')\n'); + } rom_array := ( "00000000000000000000000000000000", "00000000010010010110111111101100", "00000000100100101101000001011111", "00000000110111000001000111100011", "00000001001001010010010100001001", "00000001011011011111101001101001")>On Tue, May 26, 2009 at 12:30 PM, peng chen <rogerchan2006 at gmail.com> wrote:> Hi, R experts: > > I am trying to generate data output in the following format: > > rom_array := ( > "00000000000000000000000000000000", > "00000000010010010110111111101100", > "00000000100100101101000001011111", > "00000000110111000001000111100011", > "00000001001001010010010100001001", > "00000001011011011111101001101001") > > I have all the necessary data line, however, I am having trouble generating > the double quotation marks along with the trailing comma for each line. > > Anyone can help? > > Thanks. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
peng chen wrote:> Hi, R experts: > > I am trying to generate data output in the following format: > > rom_array := ( > "00000000000000000000000000000000", > "00000000010010010110111111101100", > "00000000100100101101000001011111", > "00000000110111000001000111100011", > "00000001001001010010010100001001", > "00000001011011011111101001101001") > > I have all the necessary data line, however, I am having trouble generating > the double quotation marks along with the trailing comma for each line. > >Hi Peng and others, I assumed that you wanted to generate that (Pascal?) expression and you have a character or binary numeric vector of the six elements. Say that vector is named "binaryvector". cat("rom_array := (\n") ends<-c(rep("\",\n",5),"\")\n") for(i in 1:length(binaryvector)) cat("\"",binaryvector[i],ends[i],sep="") should do what you want. However, I have two questions: 1) Why does cat put a newline at the end of a call with a sep argument with a newline in it? It doesn't put the sep argument after the last element of the vector, even though the help page says that it does. 2) The help page reads: sep a character vector of strings to append after each element. but cat clearly uses only the first string of the vector. Am I reading this wrongly? Jim
Maybe Matching Threads
- How to generate all permutation of 0-1 sequences in R?
- Any numeric differentiation routine in R for boundary points?
- courier to dovecot migration
- Trouble using reverse dsync backup (log in successful but almost nothing happens)
- Psych can't load LaTeX entities file