Re: [R] Use of paste with apply()

2005-11-07 Thread Thomas Lumley
On Sun, 6 Nov 2005, Kjetil Brinchmann halvorsen wrote:
examples snipped
 Why doesn't paste behave in apply as sum?


Because sum maps a vector of inputs to a single output, but paste does 
not, unless you use collapse=

-thomas

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


[R] Use of paste with apply()

2005-11-06 Thread Kjetil Brinchmann halvorsen
I was surprised by:

  test - matrix( as.character(1:4), 2)
  test
  [,1] [,2]
[1,] 1  3
[2,] 2  4
  apply(test, 1, paste, sep=+)
  [,1] [,2]
[1,] 1  2
[2,] 3  4
  apply(test, 1, paste, sep=*)
  [,1] [,2]
[1,] 1  2
[2,] 3  4
  te - matrix(1:4, 2)
  te
  [,1] [,2]
[1,]13
[2,]24
  apply(te, 1, sum)
[1] 4 6

Why doesn't paste behave in apply as sum?

Kjetil


-- 

Checked by AVG Free Edition.

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


Re: [R] Use of paste with apply()

2005-11-06 Thread Austin, Matt
The apply function is passing each row of you matrix as a single vector into
paste.  If paste receives a single vector and collapse is NULL, it will
simply coerce the vector into a character vector. 

However, when you collapse instead of sep

 test - matrix( as.character(1:4), 2)
 apply(test, 1, paste, sep=+)
 [,1] [,2]
[1,] 1  2 
[2,] 3  4 

 apply(test, 1, paste, collapse=+)
[1] 1+3 2+4

Which may be closer to what you were expecting, but I'm just guessing.

--Matt


 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] Behalf Of Kjetil 
 Brinchmann
 halvorsen
 Sent: Sunday, November 06, 2005 5:34 AM
 To: r-help@stat.math.ethz.ch
 Subject: [R] Use of paste with apply()
 
 
 I was surprised by:
 
   test - matrix( as.character(1:4), 2)
   test
   [,1] [,2]
 [1,] 1  3
 [2,] 2  4
   apply(test, 1, paste, sep=+)
   [,1] [,2]
 [1,] 1  2
 [2,] 3  4
   apply(test, 1, paste, sep=*)
   [,1] [,2]
 [1,] 1  2
 [2,] 3  4
   te - matrix(1:4, 2)
   te
   [,1] [,2]
 [1,]13
 [2,]24
   apply(te, 1, sum)
 [1] 4 6
 
 Why doesn't paste behave in apply as sum?
 
 Kjetil
 
 
 -- 
 
 Checked by AVG Free Edition.
 
 __
 R-help@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


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