Jeff Brown
2010-Mar-25 18:53 UTC
[R] c(), or cat(), or paste(), all cause unwanted reordering
Hi, I would expect the following: paste( as.character( cat( rep( ".", 2 ) ) ), "a string", as.character( cat( rep( ".", 3 ) ) ) ); to yield this string: ". . a string . . .", but instead it yields this:> . .. . .[1] " a string "The third argument has been stuck immediately after the first. The same thing happens with cat() or c(). What's going on? Thanks, Jeff -- View this message in context: n4.nabble.com/c-or-cat-or-paste-all-cause-unwanted-reordering-tp1691133p1691133.html Sent from the R help mailing list archive at Nabble.com.
Erik Iverson
2010-Mar-25 19:07 UTC
[R] c(), or cat(), or paste(), all cause unwanted reordering
Hello, Jeff Brown wrote:> I would expect the following: > > paste( > as.character( cat( rep( ".", 2 ) ) ), > "a string", > as.character( cat( rep( ".", 3 ) ) ) > ); > > to yield this string: ". . a string . . .", but instead it yields this: > >> . .. . .[1] " a string " >cat is writing its output to the console, not creating an object like you want. notice the second cat will return (and write to the console) before paste. You need the collapse argument to paste, forget about cat for this. Try: pc <- function(...) paste(..., collapse = " ") rd <- function(n) rep(".", n) pc(pc(rd(2)), "a string", pc(rd(3))) Probably many other ways. --Erik
Henrique Dallazuanna
2010-Mar-25 19:11 UTC
[R] c(), or cat(), or paste(), all cause unwanted reordering
Maybe: gsub("^(.*)$", "..\\1...", "a string") On Thu, Mar 25, 2010 at 3:53 PM, Jeff Brown <dopethatwantscash at yahoo.com> wrote:> > Hi, > > I would expect the following: > > paste( > ? ? ? ?as.character( cat( rep( ".", 2 ) ) ), > ? ? ? ?"a string", > ? ? ? ?as.character( cat( rep( ".", 3 ) ) ) > ); > > to yield this string: ". . a string . . .", but instead it yields this: > >> . .. . .[1] " a string " > > The third argument has been stuck immediately after the first. ?The same > thing happens with cat() or c(). What's going on? > > Thanks, > Jeff > > -- > View this message in context: n4.nabble.com/c-or-cat-or-paste-all-cause-unwanted-reordering-tp1691133p1691133.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Erik Iverson
2010-Mar-25 19:15 UTC
[R] c(), or cat(), or paste(), all cause unwanted reordering
Erik Iverson wrote:> Hello, > > Jeff Brown wrote: > >> I would expect the following: >> >> paste( >> as.character( cat( rep( ".", 2 ) ) ), >> "a string", >> as.character( cat( rep( ".", 3 ) ) ) >> ); >> >> to yield this string: ". . a string . . .", but instead it yields this: >> >>> . .. . .[1] " a string " >> > > cat is writing its output to the console, not creating an object like > you want. notice the second cat will return (and write to the console) > before paste. > > You need the collapse argument to paste, forget about cat for this. > > Try: > > pc <- function(...) paste(..., collapse = " ") > rd <- function(n) rep(".", n) > pc(pc(rd(2)), "a string", pc(rd(3))) > >Or even a little bit "DRY"er: pc <- function(...) paste(..., collapse = " ") rd <- function(n) pc(rep(".", n)) pc(rd(2), "a string", rd(3))
Sarah Goslee
2010-Mar-25 19:15 UTC
[R] c(), or cat(), or paste(), all cause unwanted reordering
cat() isn't doing what you think, and as.character() is unnecessary here because paste() takes care of that. A couple of more elegant solutions have already appeared, but here's one that parallels your version: paste(c( rep( ".", 2 ), "a string", rep( ".", 3 )), collapse=" ") On Thu, Mar 25, 2010 at 2:53 PM, Jeff Brown <dopethatwantscash at yahoo.com> wrote:> > Hi, > > I would expect the following: > > paste( > ? ? ? ?as.character( cat( rep( ".", 2 ) ) ), > ? ? ? ?"a string", > ? ? ? ?as.character( cat( rep( ".", 3 ) ) ) > ); >-- Sarah Goslee functionaldiversity.org
Jeff Brown
2010-Mar-25 19:54 UTC
[R] c(), or cat(), or paste(), all cause unwanted reordering
Wow, you guys are awesome. Thanks! -- View this message in context: n4.nabble.com/c-or-cat-or-paste-all-cause-unwanted-reordering-tp1691133p1691198.html Sent from the R help mailing list archive at Nabble.com.
Nikos Alexandris
2010-Apr-06 14:56 UTC
[R] c(), or cat(), or paste(), all cause unwanted reordering
On Thu, 2010-03-25 at 11:54 -0800, Jeff Brown wrote:> Wow, you guys are awesome. Thanks!Thanks for the "cat()" question Jeff and to all guRus out there for the replies. This is something I was looking for the last hour. Nikos