Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't comparison between numeric and character variables give a warning?

Tags:

r

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?

like image 569
fxrhvk Avatar asked Sep 25 '13 16:09

fxrhvk


People also ask

How do you convert a numeric variable to a character variable?

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.

What is the difference between character and factor in R?

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.

What are character variables?

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.

How do I change a variable to character in SAS?

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.);


1 Answers

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.

like image 114
Peyton Avatar answered Oct 19 '22 05:10

Peyton