Received your offlist question and see that I did not understand your request. See below for another alternative
On May 28, 2011, at 7:53 AM, David Winsemius wrote:


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

If you want to replace an entry in a character-mode table whose value == "NA" (which is not a special missing value in that mode)

> is.na("NA")
[1] FALSE
> is.na(NA_character_)
[1] TRUE

.... , then this should work:

ft.[which(ft.=="NA")] <- "my.Command.To.Deal.With.NA"
ft.
Survived No Yes
Class Sex    Age
1st   Male   Child          my.Command.To.Deal.With.NA 5.0
             Adult          118.0                      57.0
      Female Child          0.0                        1.0
             Adult          4.0                        140.0
2nd   Male   Child          0.0                        11.0
             Adult          154.0                      14.0
      Female Child          0.0                        13.0
             Adult          13.0                       80.0
3rd   Male   Child          35.0                       13.0
             Adult          387.0                      75.0
      Female Child          17.0                       14.0
             Adult          89.0                       76.0
Crew  Male   Child          0.0                        0.0
             Adult          670.0                      192.0
      Female Child          0.0                        0.0
             Adult          3.0                        20.0


Although this messes up the header alignment. At least it "finds" the "NA".
--
David.


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

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to