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

Marius Hofert m_hofert at web.de
Sat May 28 13:19:30 CEST 2011


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.

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



More information about the R-help mailing list