I had a bug in my code resulting from an inadvertent comparison between a character variable and a numeric variable (they were both supposed to be numeric). This bug would have been much easier to find if R had a warning when doing this type of comparison. For example, why does this not throw a warning
> 'two' < 5
[1] FALSE
but this does throw a warning
> as.numeric('two') < 5
[1] NA
Warning message:
NAs introduced by coercion
It is not clear to me what is going on behind the scenes in the first comparison?
To convert numeric values to character, use the PUT function: new_variable = put(original_variable, format.); The format tells SAS what format to apply to the value in the original variable. The format must be of the same type as the original variable.
The main difference is that factors have predefined levels. Thus their value can only be one of those levels or NA. Whereas characters can be anything.
Character variables (also known as string variables) contain information that the system recognizes as text. This can include letters, special characters (such as parentheses or pound signs), and even numbers.
You can use the put() function in SAS to convert a numeric variable to a character variable. This function uses the following basic syntax: character_var = put(numeric_var, 8.);
In your example 5
is converted to a character, so the test is the same as 'two' < as.character(5)
.
From ?Comparison
:
If the two arguments are atomic vectors of different types, one is coerced to the type of the other, the (decreasing) order of precedence being character, complex, numeric, integer, logical and raw.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With