Create double object with NA - r

How can I create a double object with an NA value.
I am writing a test case where the output is NA:
gt[2]$height
[1] NA
typeof(gt[2])
> "double"
Question is how can I create an object of type "double" with an NA value.

By default, NA is a logical constant of length 1 used to represent missing values in data, and the type of NA can be modified by using one of the four types of NA such as NA_integer_, NA_real_, NA_complex_ and NA_character_.
For more info, please read the documentation page of ?NA
Try this:
x <- numeric()
typeof(x)
# [1] "double"
y <- NA_real_
typeof(y)
# [1] "double"
y
# [1] NA

mydata<-data.frame(height=NA)
mydata$height<-as.double(mydata$height)
typeof(mydata$height)

Related

NA in List vs. Vector

Can someone explain why NA is evaluated as TRUE when it's inside a vector, but FALSE when it's an element in a list? See below.
Big thanks for any help.
> is.numeric(c(1, 2, NA)[3])
[1] TRUE
> is.numeric(list(1, 2, NA)[[3]])
[1] FALSE
> typeof(c(1, 2, NA)[3])
[1] "double"
typeof(list(1, 2, NA)[[3]])
[1] "logical"
It is related to type coercion in the vector to numeric as vector can have only a single type whereas each list element can be of different type. By default, the NA is logical. According to ?NA
NA is a logical constant of length 1 which contains a missing value indicator. NA can be coerced to any other vector type except raw. There are also constants NA_integer_, NA_real_, NA_complex_ and NA_character_ of the other atomic vector types which support missing values: all of these are reserved words in the R language.
If we want numeric NA in a list, use NA_real_
typeof(list(1, 2, NA_real_)[[3]])
[1] "double"

R: Accept commandline input, Show Error if it is not numeric type [duplicate]

I generally prefer to code R so that I don't get warnings, but I don't know how to avoid getting a warning when using as.numeric to convert a character vector.
For example:
x <- as.numeric(c("1", "2", "X"))
Will give me a warning because it introduced NAs by coercion. I want NAs introduced by coercion - is there a way to tell it "yes this is what I want to do". Or should I just live with the warning?
Or should I be using a different function for this task?
Use suppressWarnings():
suppressWarnings(as.numeric(c("1", "2", "X")))
[1] 1 2 NA
This suppresses warnings.
suppressWarnings() has already been mentioned. An alternative is to manually convert the problematic characters to NA first. For your particular problem, taRifx::destring does just that. This way if you get some other, unexpected warning out of your function, it won't be suppressed.
> library(taRifx)
> x <- as.numeric(c("1", "2", "X"))
Warning message:
NAs introduced by coercion
> y <- destring(c("1", "2", "X"))
> y
[1] 1 2 NA
> x
[1] 1 2 NA
In general suppressing warnings is not the best solution as you may want to be warned when some unexpected input will be provided.
Solution below is wrapper for maintaining just NA during data type conversion. Doesn't require any package.
as.num = function(x, na.strings = "NA") {
stopifnot(is.character(x))
na = x %in% na.strings
x[na] = "0"
x = as.numeric(x)
x[na] = NA_real_
x
}
as.num(c("1", "2", "X"), na.strings="X")
#[1] 1 2 NA
I have slightly modified the jangorecki function for the case where we may have a variety of values that cannot be converted to a number. In my function, a template search is performed and if the template is not found, FALSE is returned.! before gperl, it means that we need those vector elements that do not match the template. The rest is similar to the as.num function. Example:
as.num.pattern <- function(x, pattern){
stopifnot(is.character(x))
na = !grepl(pattern, x)
x[na] = -Inf
x = as.numeric(x)
x[na] = NA_real_
x
}
as.num.pattern(c('1', '2', '3.43', 'char1', 'test2', 'other3', '23/40', '23, 54 cm.'))
[1] 1.00 2.00 3.43 NA NA NA NA NA

How do I prevent R from coercing this vector of Dates to numeric?

When combining a date vector with an NA, R will coerce the whole vector to numeric if NA appears first. If NA does not appear first it will coerce to Date.
x <- Sys.Date()
c(NA, x)
# [1] NA 16248
c(x, NA)
# [1] "2014-06-27" NA
How can I make it coerce to Date always, regardless of the order the NAs appear? Secondly, what if I do not know the type of x, how can I still be certain that it coerces to the class of the vector x and not numeric?
This is the result of S3 method dispatch acting on the first argument NA and hence the default method is used which coerces everything up to numerics. The solution is to be explicit about the method to call, in this case c.Date():
x <- Sys.Date()
xx <- c.Date(c.Date(NA, x))
xx
class(xx)
> xx
[1] NA "2014-06-27"
> class(xx)
[1] "Date"

In R, why does subtracting numerics from NA return NA but subtracting dates from NA return an error?

In R, if you subtract a numeric from NA it will return NA:
> x <- 1
> NA - x
[1] NA
But if you try to subtract a date from NA it returns an error:
> x <- as.Date("2014-04-22")
> NA - x
Error in `-.Date`(NA, x) : can only subtract from "Date" objects
I am interested in why R returns an error. As I understand it, "Date" objects are just a heyrepresentation of the integer difference from the origin (defaulting to 1970-01-01).
This should work:
> as.Date(NA)-as.Date("2014-04-22")
Time difference of NA days
You may, however, subtract an integer from a date:
> as.Date("2014-04-22")-NA
[1] NA
> as.Date("2014-04-22")-2
[1] "2014-04-20"
The reason for this is that the - operator is in fact an S3 method -- ``overloading'' (invoked method selection) is done according to the type of its first argument. Try:
> get("-.Date")
If this was an S4 method, it could have been overloaded to take the type of the second argument into account.
For more details on S3 method dispatch mechanism, see here.

Why is there no NA_logical_

From help("NA"):
There are also constants NA_integer_, NA_real_, NA_complex_ and
NA_character_ of the other atomic vector types which support missing
values: all of these are reserved words in the R language.
My question is why there is no NA_logical_ or similar, and what to do about it.
Specifically, I am creating several large very similar data.tables, which should be class compatible for later rbinding. When one of the data.tables is missing a variable, I am creating that column but with it set to all NAs of the particular type. However, for a logical I can't do that.
In this case, it probably doesn't matter too much (data.table dislikes coercing columns from one type to another, but it also dislikes adding rows, so I have to create a new table to hold the rbound version anyway), but I'm puzzled as to why the NA_logical_, which logically should exist, does not.
Example:
library(data.table)
Y <- data.table( a=NA_character_, b=rep(NA_integer_,5) )
Y[ 3, b:=FALSE ]
Y[ 2, a:="zebra" ]
> Y
a b
1: NA NA
2: zebra NA
3: NA 0
4: NA NA
5: NA NA
> class(Y$b)
[1] "integer"
Two questions:
Why doesn't NA_logical_ exist, when its relatives do?
What should I do about it in the context of data.table or just to avoid coercion as much as possible? I assume using NA_integer_ buys me little in terms of coercion (it will coerce the logical I'm adding in to 0L/1L, which isn't terrible, but isn't ideal.
NA is already logical so NA_logical_ is not needed. Just use NA in those situations where you need a missing logical. Note:
> typeof(NA)
[1] "logical"
Since the NA_*_ names are all reserved words there was likely a desire to minimize the number of them.
Example:
library(data.table)
X <- data.table( a=NA_character_, b=rep(NA,5) )
X[ 3, b:=FALSE ]
> X
a b
1: NA NA
2: NA NA
3: NA FALSE
4: NA NA
5: NA NA
I think based on this
#define NA_LOGICAL R_NaInt
from $R_HOME/R/include/R_ext/Arith.h we can suggest using NA_integer or NA_real and hence plain old NA in R code:
R> as.logical(c(0,1,NA))
[1] FALSE TRUE NA
R>
R> as.logical(c(0L, 1L, NA_integer_))
[1] FALSE TRUE NA
R>
which has
R> class(as.logical(c(0,1,NA)))
[1] "logical"
R>
R> class(as.logical(c(0, 1, NA_real_)))
[1] "logical"
R>
Or am I misunderstanding your question? R's logical type is three-values: yay, nay and missing. And we can use the NA from either integer or numeric to cast. Does that help?

Resources