[R] Convert NA to '.'

David Winsemius dwinsemius at comcast.net
Mon Jun 22 19:45:11 CEST 2009


On Jun 22, 2009, at 1:21 PM, Jorge Ivan Velez wrote:

> Hi David,
> It works for me when handling data frames mixing characters and  
> numeric by
> using either print() or a function called "foo":
>
> # Some data
> x <- c("A", "B", "C", 3.5, 1, 0, NA, "a character",NA, "another  
> character")
> mydata <- data.frame(x = x, y = sample(x))
> mydata

Those are not numeric. They are factors.
>
> # Option 1: print()ing
> print(mydata, na.print = ".")

Does not work as expected with numeric NA's.

  mydata <- data.frame(x = x, y = sample(x),  
c=sample(c(1:2,NA),length(x),replace=TRUE))
  print(mydata, na.print = ".")
                    x                 y  c
1                  A                 . NA
2                  B                 . NA
3                  C another character NA
4                3.5                 0  1
5                  1                 B  1
6                  0                 C  1
7                  .                 1  1
8        a character       a character  1
9                  .               3.5  2
10 another character                 A  1


?format.data.frame


>
> # Option 2: using a function "foo"
> foo <- function(x){
>        y <- as.character(x)
>        y[is.na(y)] <- "."
>        y
>        }
>
> mydata[,] <- apply(mydata, 2, foo)
> mydata
>

But this does work, even when when given numeric NA's

  mydata[,] <- apply(mydata, 2, foo)
  mydata
                    x                 y  c
1                  A                 .  .
2                  B                 .  .
3                  C another character  .
4                3.5                 0  1
5                  1                 B  1
6                  0                 C  1
7                  .                 1  1
8        a character       a character  1
9                  .               3.5  2
10 another character                 A  1

>
>
> On Mon, Jun 22, 2009 at 9:22 AM, David_D <david.dabin at gmail.com>  
> wrote:
>
>>
>> Jorge,
>>
>> Thanks a lot for your reply. It's indeed working in this case.  
>> However,
>> with
>> data frames mixing numeric and character is not working anymore.  
>> Morever I
>> am working with many variables and I don't want to modify them.  
>> What I
>> would
>> really appreciate is a global option (in the Rprofile?) that allow to
>> change
>> the display nd printing of NA by any symbol?
>>
>> Does it exist?
>>
>> Best regards,
>> David
>>
>>
>> Jorge Ivan Velez wrote:
>>>
>>> Dear David,
>>> Try this:
>>>
>>> x <- c("A", "B", "C", NA)
>>> x[is.na(x)] <- "."
>>> x
>>>
>>> HTH,
>>>
>>> Jorge
>>>
>>> On Mon, Jun 22, 2009 at 2:11 AM, David_D <david.dabin at gmail.com>  
>>> wrote:
>>>
>>>>
>>>> Dear R-users,
>>>>
>>>> For reporting purpose (using Sweave and LaTeX), I am creating  
>>>> complex
>>>> tables
>>>> with the cat function such as
>>>>
>>>>> x<-c("A", "B", "C", NA)
>>>>> cat(x, '\n')
>>>> A B C NA
>>>>
>>>> For convenience, I would like to change all my NA value to  
>>>> something
>> else
>>>> like '.' (as in SAS for example). Is there a global option which  
>>>> allows
>>>> this
>>>> change? Or should I change all my code to work with the print  
>>>> function
>>>> and
>>>> the na.print argument?
>>>>
>>>> Best regards,
>>>> David
>>>> --
>>>> View this message in context:
>>>> http://www.nabble.com/Convert-NA-to-%27.%27- 
>>>> tp24142187p24142187.html
>>>> Sent from the R help mailing list archive at Nabble.com.
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>>>      [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Convert-NA-to-%27.%27-tp24142187p24147157.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list