[R] Summary: paste with a matrix

Jens Oehlschlägel-Akiyoshi jens.oehlschlaegel-akiyoshi at mdfactory.de
Wed Jan 26 17:55:09 CET 2000



I asked how to paste a matrix without using eval().
Thank you Martyn Plummer and Peter Malewski for reminding me of apply().

apply() is under certain conditions not as fast as eval(), but apply() seems
to use less memory and probably should be preferred.

Thanks also to Peter Dalgaard for warning me to rather not use eval() in
functions.

Functions and timings below.

Regards


Jens Oehlschlägel-Akiyoshi


# Version with apply()
paste.matrix <- function(mtext, sep=" ", collapse=NULL){
  if (is.null(collapse))
     apply(mtext, 1, paste, collapse=sep)
  else
     paste(apply(mtext, 1, paste, collapse=sep), collapse=collapse)
}

# Version with eval() in function frame
paste.matrix2 <- function(mtext, sep=" ", collapse=NULL){
  rcode <- paste(
      "paste("
    , paste("mtext[,", 1:ncol(mtext), "]", sep="", collapse=",")
    , ", sep=sep, collapse=collapse)"
  , sep="")
  eval(parse(text=rcode))
}

# Version with eval() in parent frame
paste.matrix3 <- function(mtext, sep=" ", collapse=NULL){
  rcode <- paste(
      "paste("
    , paste(deparse(substitute(mtext)), "[,", 1:ncol(mtext), "]", sep="",
collapse=",")
    , ", sep="
    , if (is.null(sep)) "NULL" else if (sep=='"') quotate(sep, "'") else
quotate(sep, '"')
    , ", collapse="
    , if (is.null(collapse)) "NULL" else if (collapse=='"')
quotate(collapse, "'") else quotate(collapse, '"')
    , ")"
  , sep="")
  eval(parse(text=rcode), envir=parent.frame())
}


> system.time(paste.matrix(matrix(1:(10000), ncol=10)))
[1] 1.04 0.00 1.04   NA   NA
> system.time(paste.matrix2(matrix(1:(10000), ncol=10)))
[1] 0.07 0.00 0.07   NA   NA
> system.time(paste.matrix3(matrix(1:(10000), ncol=10)))
[1] 0.24 0.00 0.24   NA   NA
> system.time(paste.matrix(matrix(1:(10000), ncol=1000)))
[1] 0.09 0.00 0.09   NA   NA
> system.time(paste.matrix2(matrix(1:(10000), ncol=1000)))
[1] 0.61 0.00 0.61   NA   NA
> system.time(paste.matrix3(matrix(1:(10000), ncol=1000)))
[1] 7.74 0.00 7.74   NA   NA
> system.time(paste.matrix(matrix(1:(100000), ncol=10)))
[1] 11.44  0.04 11.48    NA    NA
> system.time(paste.matrix2(matrix(1:(100000), ncol=10)))
Error: cons memory (250000 cells) exhausted
       See "help(Memory)" on how to increase the number of cons cells.
> system.time(paste.matrix3(matrix(1:(100000), ncol=10)))
Error: cons memory (250000 cells) exhausted
       See "help(Memory)" on how to increase the number of cons cells.



> version
         _
platform Windows
arch     x86
os       Win32
system   x86, Win32
status
major    0
minor    90.1
year     1999
month    December
day      15
language R

> gc()
         free  total (Mb)
Ncells  73829 250000  4.8
Vcells 705159 786432  6.0

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