[R] paste with a matrix

Jens Oehlschlägel-Akiyoshi jens.oehlschlaegel-akiyoshi at mdfactory.de
Wed Jan 26 15:15:02 CET 2000



Hi,

below is a function which pastes a matrix, but uses parsing
(deparse(substitute()) and eval()).
Does anyone know a more standard solution to pasting a matrix?

Best


Jens



> paste.matrix(dd, sep=" ", collapse=NULL)
[1] "1 a" "2 b" "3 c"
> paste.matrix <- function(mtext, sep=" ", collapse=NULL){
+   rcode <- paste(
+       "paste("
+     , paste(deparse(substitute(mtext)), "[,", 1:ncol(mtext), "]", sep="",
collapse=",")
+     , ", sep=sep, collapse=collapse)"
+   , sep="")
+   eval(parse(text=rcode))
+ }

> dd <- data.frame(code=c(1:3), label=letters[1:3])

> paste.matrix(dd, sep=" ", collapse=NULL)
[1] "1 a" "2 b" "3 c"

> paste.matrix(t(dd), sep=" ", collapse=NULL)
[1] "1 2 3" "a b c"




--
Dr. Jens Oehlschlägel-Akiyoshi
MD FACTORY GmbH
Bayerstrasse 21

80335 München

Tel.: 089 545 28-27
Fax.: 089 545 28-10
http://www.mdfactory.de

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list