From hadley at rice.edu  Wed May  5 18:25:48 2010
From: hadley at rice.edu (Hadley Wickham)
Date: Wed, 5 May 2010 11:25:48 -0500
Subject: [Rd] Generate NA of specified type
In-Reply-To: <h2h66f3bd911005050916nf9007da5h96d64ad4430c7de2@mail.gmail.com>
References: <h2gf8e6ff051005050857tfe7d7566m797b3abbad5f429c@mail.gmail.com> 
	<h2h66f3bd911005050916nf9007da5h96d64ad4430c7de2@mail.gmail.com>
Message-ID: <s2nf8e6ff051005050925w4ee05f43xc052aa4141a4a71b@mail.gmail.com>

> What about NAof <- function(x) as(NA, class(x))?
>

That's pretty close, but it doesn't fail nicely for non-atomics:

> as(NA, class(Sys.Date()))
Error in as(NA, class(Sys.Date())) :
  no method or default for coercing "logical" to "Date"

vs.

> as.Date(NA)
[1] NA

But I'm probably being too fussy (especially since I didn't ask for
that in my original post ;).  I'm currently using this:

NAof <- function(x) {
  length(x) <- 1
  is.na(x) <- TRUE
  x
}

It's rather lacking in elegance :(

Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



[Rd] Generate NA of specified type
Hadley Wickham