[R] ftable: how to replace NA and format entries without changing their mode?

David Winsemius dwinsemius at comcast.net
Sat May 28 13:53:43 CEST 2011


On May 28, 2011, at 7:19 AM, Marius Hofert wrote:

> Dear all,
>
> another ftable problem, now related to formatC.
> One typically would like to format entries in an ftable (adjust  
> digits, replace NA, ...)
> before format() is applied to convert the formatted ftable to an  
> object which
> xtable can deal with. The output of xtable can then be used within a  
> LaTeX table.

 > 1/3
[1] 0.3333333
 > options(digits=3)
 > 1/3
[1] 0.333

>
> The problem I face is that the ftable entries [numeric] change their  
> mode when
> one of the operations "adjust digits" or "replace NA" is applied.  
> Here is a
> minimal example:
>
> ## first adjusting the format, then trying to remove NA
> (ft <- ftable(Titanic)) # ftable
> ft[1,1] <- NA # create an NA entry to show the behavior
> is.numeric(ft) # => is numeric
> ft. <- formatC(ft, digits=1, format="f") # adjust format
> is.numeric(ft.) # => not numeric anymore => one can not further use  
> is.na() etc.
> ft.[is.na(ft.)] <- "my.Command.To.Deal.With.NA" # does not work  
> because is.na() does not find NA
> ft. # (of course) still contains NA
>
> ## first remove NA, then trying to adjust the format
> (ft <- ftable(Titanic)) # ftable
> ft[1,1] <- NA
> ft[is.na(ft)] <- "my.LaTeX.Code.To.Deal.With.NA"
> is.character(ft) # => now character, adjusting the format of the  
> numbers with formatC not possible anymore
> ft
> formatC(ft, digits=1, format="f") # (of course) not working anymore
>
> How can I accomplish both (example-)tasks without changing the mode
> of the ftable entries?
> Note: I would like to keep the ftable structure since this nicely  
> converts to a
> LaTeX table later on.
>
> Cheers,
>
> Marius
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list