[R] as.character limits length of result for formula

William Dunlap wdunlap at tibco.com
Wed Jun 1 21:51:13 CEST 2011


as.character() doesn't give a faithful character
representation of its input for lots of language-
related inputs.  E.g.,
  > f <- reformulate(paste(sep="","X",1:500), quote(log(Y)))
  > cat(strwrap(as.character(f), 60), sep="\n")
  ~
  log(Y)
  X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 +
  X12 + X13 + X14 + X15 + X16 + X17 + X18 + X19 + X20 + X21 +
  X22 + X23 + X24 + X25 + X26 + X27 + X28 + X29 + X30 + X31 +
  X32 + X33 + X34 + X35 + X36 + X37 + X38 + X39 + X40 + X41 +
  X42 + X43 + X44 + X45 + X46 + X47 + X48 + X49 + X50 + X51 +
  X52 + X53 + X54 + X55 + X56 + X57 + X58 + X59 + X60 + X61 +
  X62 + X63 + X64 + X65 + X66 + X67 + X68 + X69 + X70 + X71 +
  X72 + X73 + X74 + X75 + X76 + X77 + X78 + X79 + X80 + X81 +
  X82 + X83 + X84 + X85 +
(Yes, there is nothing after the "X85 +".)

deparse() works better for such objects.  You may want to use
paste(collapse="\n") on its output to make a single string.
  > deparse(f)
   [1] "log(Y) ~ X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11
+ " 
   [2] "    X12 + X13 + X14 + X15 + X16 + X17 + X18 + X19 + X20 + X21 +
"   
   ... lots of text omitted ...
  [53] "    X480 + X481 + X482 + X483 + X484 + X485 + X486 + X487 + X488
+ "
  [54] "    X489 + X490 + X491 + X492 + X493 + X494 + X495 + X496 + X497
+ "
  [55] "    X498 + X499 + X500"     


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Terrence Ireland
> Sent: Wednesday, June 01, 2011 12:19 PM
> To: r-help at r-project.org
> Subject: [R] as.character limits length of result for formula
> 
> If you want a character representation of a long formula (or
> 
> a formula with long names), you can use:
> 
>  
> 
> as.character(my.formula)
> 
>  
> 
> However restriction on length of an as.character result
> 
> returns only the beginning of a long formula, and without comment.
> 
>  
> 
> In most cases, the following expression provides the complete
> 
> result:
> 
>  
> 
> paste(my.formula[[2]], "  ~ ", paste(attr(terms(my.formula),
> "term.labels"),collapse= " + "),sep="")
> 
>  
> 
> It would be better to make as.character handle any size formula.
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list