Undefined values: Difference between revisions
Content added Content deleted
m (→{{header|C sharp|C#}}: Fixed Code and Lang blocks) |
(Added R code) |
||
Line 341: | Line 341: | ||
Done |
Done |
||
</pre> |
</pre> |
||
=={{header|R}}== |
|||
There are four cases to consider. To test whether a varaible has previously been defined, use <code>exists</code>. |
|||
<lang r> |
|||
exists("x") |
|||
</lang> |
|||
If you want to declare a variable with undefined contents, use <code>NULL</code>. |
|||
<lang r> |
|||
x <- NULL |
|||
</lang> |
|||
If you want to declare a variable with missing values, use <code>NA</code>. |
|||
<lang r> |
|||
y <- c(1, 4, 9, NA, 25) |
|||
z <- c("foo", NA, "baz") |
|||
</lang> |
|||
(Note that there are different types of <code>NA</code>, namely <code>NA_integer_</code>, <code>NA_real_</code>, <code>NA_character_</code>, <code>NA_complex_</code> and plain (logical) <code>NA</code>. In practice, you should hardly ever need to explicitly set which type of NA you are using, as it will be done automatically.) |
|||
Finally, you test for arguments that haven't been passed into a function with <code>missing</code>. |
|||
<lang r> |
|||
print_is_missing <- function(x) |
|||
{ |
|||
print(missing(x)) |
|||
} |
|||
print_is_missing() # TRUE |
|||
print_is_missing(123) # FALSE |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |