Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-04-06 Thread Nikos Alexandris
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

__
R-help@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-04-06 Thread Nikos Alexandris
Jeff Brown wrote:
  Wow, you guys are awesome.  Thanks!

Nikos Alexandris wrote:
 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.

I can't seem to make this run:

I have a function ( Column.of.Matrix.1 , Column.of.Matrix.2 ) which
gives me an output like:

--%---
Divergence: 0.2605 
Jeffries-Matusita:  0.04489 
Bhattacharryya: 0.0227 
Transformed divergence: 0.06406
--%---

The custom function uses cat() as well to print the above. Now, I
would like to use this function in a for() loop and print before the
results a message like Separability measures between A and B:. I get
A and B using colnames(Matrix.X[i]).

Everything seems to be ok except that the message is printed after the
results. Although I read all relevant posts in this thread about
paste(), cat(), c(), I still don't understand why the message is being
printed in the end when I use cat( c( ...)) or paste ( c(...)) or
other combinations:

--%---
cat ( c (
Separability measures between: ,
1st sample ,  and  , 2nd sample , \n ,
function ( Matrix.1 [i] , Matrix.2 [i] ) ,
collapse =  
) )
--%---

Can I *force* the output of the function to be printed in the end?
Thank you, Nikos

__
R-help@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-04-06 Thread Nikos Alexandris
On Tue, 2010-04-06 at 19:29 +0200, Nikos Alexandris wrote:
 Jeff Brown wrote:
   Wow, you guys are awesome.  Thanks!
 
 Nikos Alexandris wrote:
  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.
 
 I can't seem to make this run:
 
 I have a function ( Column.of.Matrix.1 , Column.of.Matrix.2 ) which
 gives me an output like:
 
 --%---
 Divergence: 0.2605 
 Jeffries-Matusita:  0.04489 
 Bhattacharryya: 0.0227 
 Transformed divergence: 0.06406
 --%---
 
 The custom function uses cat() as well to print the above. Now, I
 would like to use this function in a for() loop and print before the
 results a message like Separability measures between A and B:. I get
 A and B using colnames(Matrix.X[i]).
 
 Everything seems to be ok except that the message is printed after the
 results. Although I read all relevant posts in this thread about
 paste(), cat(), c(), I still don't understand why the message is being
 printed in the end when I use cat( c( ...)) or paste ( c(...)) or
 other combinations:
 
 --%---
 cat ( c (
   Separability measures between: ,
   1st sample ,  and  , 2nd sample , \n ,
   function ( Matrix.1 [i] , Matrix.2 [i] ) ,
   collapse =  
 ) )
 --%---
 
 Can I *force* the output of the function to be printed in the end?
 Thank you, Nikos

Got it by using a new function which first calls cat(...) and then the
custom.function that gives what I need.

Thanks, Nikos

__
R-help@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.


[R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Jeff Brown

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: 
http://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@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Erik Iverson

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

__
R-help@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Henrique Dallazuanna
Maybe:

gsub(^(.*)$, ..\\1..., a string)

On Thu, Mar 25, 2010 at 3:53 PM, Jeff Brown dopethatwantsc...@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: 
 http://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@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
R-help@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Erik Iverson



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 DRYer:

pc - function(...) paste(..., collapse =  )
rd - function(n) pc(rep(., n))
pc(rd(2), a string, rd(3))

__
R-help@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Ted Harding
On 25-Mar-10 19:07:23, 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)))
 
 Probably many other ways.
 --Erik

One needs to be very circumspect with this sort of thing! For instance,
experimenting with simplifications of Jeff's expression:

  paste(
  rep( ., 2 ),  
  a string,
  rep( ., 3 )
  )

  # [1] . a string . . a string . . a string .

Here, it seems to be recycling the length-2 and length-3 vectors
rep( ., 2 ) and rep( ., 3 ) around the length-1 vector a string!

Ted.


E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
Fax-to-email: +44 (0)870 094 0861
Date: 25-Mar-10   Time: 19:19:08
-- XFMail --

__
R-help@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Sarah Goslee
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 dopethatwantsc...@yahoo.com wrote:

 Hi,

 I would expect the following:

 paste(
        as.character( cat( rep( ., 2 ) ) ),
        a string,
        as.character( cat( rep( ., 3 ) ) )
 );


-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Sarah Goslee
On Thu, Mar 25, 2010 at 3:19 PM, Ted Harding
ted.hard...@manchester.ac.uk wrote:

 One needs to be very circumspect with this sort of thing! For instance,
 experimenting with simplifications of Jeff's expression:

  paste(
          rep( ., 2 ),
          a string,
          rep( ., 3 )
  )

  # [1] . a string . . a string . . a string .
 Here, it seems to be recycling the length-2 and length-3 vectors
 rep( ., 2 ) and rep( ., 3 ) around the length-1 vector a string!

Yes, because you are pasting
a vector of length 2
a vector of length 1
a vector of length 3

R has no idea what you mean. paste() by default recycles
elements to match the longest vector. Paste tje first element
of the first vector, the first element of the second, then the
first element of the third into the first result string.
Paste the second element of the first vector, the second of the
second... etc.
With elements recycled as necessary.

Careful use of c() gives you the expected results:

 v1 - c(a, a)
 v2 - c(b)
 v3 - c(c, c, c)

 paste(v1, v2, v3)
[1] a b c a b c a b c
 paste(v1, v2, v3, collapse= )
[1] a b c a b c a b c


 paste(c(v1, v2, v3))
[1] a a b c c c

 paste(c(v1, v2, v3), collapse= )
[1] a a b c c c

 paste(c(v1, v2), v3)
[1] a c a c b c

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@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.


Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Jeff Brown

Wow, you guys are awesome.  Thanks!
-- 
View this message in context: 
http://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.

__
R-help@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.