Domanda

I'm trying to load data into a data frame in R from a state education Access database. The database provides information about schools in the state, and each school has a 12-digit identifying number. When I load the table with

demo.factors <- sqlQuery(connection, "SELECT * FROM 'Demographic Factors' WHERE YEAR = 2010")

it converts this number to what appears to be scientific notation (my apologies for not having a good grasp of the inner workings here). When I try to convert them back to integers as they were in the database using

demo.factors$ENTITY_CD <- as.integer(demo.factors$ENTITY_CD)

I get a bunch of NAs and integers that do not match the original.

Is there another data type I should be using? Is there a way to either recover the original ints or to import them from the start?

Thank you so much for your help!

È stato utile?

Soluzione

Your issues is that an integer value can only handle numbers up to 2*10^9 (i.e. ~9 digits long). Your school id numbers are 12 digits long, and thus should are of class numeric or double.

But this is all irrelevant, since the number will be handled correctly by R.

It seems to me that you have an issue with the display of this number. You have a number of choices. Here are a few:

Display the numbers as a character string:

x=999999999999
as.character(x)
[1] "999999999999"

Use any of the string formatting functions to specify a format. This will still convert the value to display as a character, but gives you flexibility of the formatting. For example, use format:

format(x, scientific=FALSE, width=12)
[1] " 999999999999"

Other functions that perform similar formatting include sprintf, prettyNum and formatC.

In a nutshell, don't try and do type conversion. Simply use one of these functions when you want to display your results. If you really want to do a type conversion, then I suggest you convert the number to as.character() - the reason is that I suspect you will never do arithmetic on these numbers, so they are essentially character strings.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top