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

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sat, 28 May 2011 07:53:43 -0400

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



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. Received on Sat 28 May 2011 - 11:58:06 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 28 May 2011 - 13:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive