jmbejara / comp-econ-sp18

Main Course Repository for Computational Methods in Economics (Econ 21410, Spring 2018)
16 stars 23 forks source link

How to convert strings to float in R? #66

Closed Jacob-Bishop closed 6 years ago

Jacob-Bishop commented 6 years ago

Type.convert() seems to convert to numeric, but it doesn't have an argument to specify which kind of numeric value, and defaults to double.

jmbejara commented 6 years ago

To complete this exercise, I used a combination of the functions gsub and as.numeric.

Jacob-Bishop commented 6 years ago

The documentation I found (see below) seems to suggest as.numeric also produces doubles. Am I missing something?

https://www.rdocumentation.org/packages/base/versions/3.5.0/topics/numeric

jmbejara commented 6 years ago

I don't think that base R supports floats. The point of the assignment is to change from a string representation to a floating point number---whether that's a float or a double is unimportant. Plus, a Python float is not the same thing as a float vs double in a language like, say, C or C++.

Jacob-Bishop commented 6 years ago

Interesting, I didn't know that.

Does that hold for numpy/Pandas too, or does dtype=float give c types?

jmbejara commented 6 years ago

It's a little more complicated than that. For example, Numpy defines it's own data types. But Numpy is built in C. Python (the standard implementation) is also built in C. Some data types are platform dependent. (We once discussed in class how this might depend on whether your machine has a 32 or 64 bit architecture, etc.)

I'm don't really know the details. You might check these out: