[R] Use of paste with apply()
Austin, Matt
maustin at amgen.com
Mon Nov 7 03:33:27 CET 2005
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: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Kjetil
> Brinchmann
> halvorsen
> Sent: Sunday, November 06, 2005 5:34 AM
> To: r-help at 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,] 1 3
> [2,] 2 4
> > apply(te, 1, sum)
> [1] 4 6
>
> Why doesn't paste behave in apply as sum?
>
> Kjetil
>
>
> --
>
> Checked by AVG Free Edition.
>
> ______________________________________________
> 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
>
More information about the R-help
mailing list