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 09:22:55 -0400

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_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 - 13:25:17 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