Thursday, February 11, 2010

Reading Currency values in [R]?

Apparently [R] doesn't have any base funcitons for interpreting numbers with comma separators and dollar signs in front of them. It reads them as text strings; and thus as factors. Try and coerce them into "numeric" type and it returns some garbage number (the index of the factor, I think). What the everlovingeff is that all about? Even Excel can handle currency-formatted numbers without blinking; you'd think the language designed specifically for high-performance data analysis would be able to recognize that "$10,112.43" is likely intended to be read as a number.

Of course, its easy enough to write a function that strips out the dollar signs and commas and converts it to the correct number. Maybe the [R] authors intentionally omitted that simple piece of functionality specifically so that I'd have a reason to learn something about regular expressions and the apply() function. Or maybe I just missed it.