Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is R reading numeric data as character?

Tags:

r

I'm trying to load a file that contains integers and float data. I'm at a loss as to why R will read one of the columns as a character field.

 > df <- read.table( 'C:\\temp\\test.tab' , 
 +                   sep = '\t' , header = TRUE , stringsAsFactors = FALSE , dec="." )
 > str(df)
 'data.frame':  7 obs. of  5 variables:
  $ A: int  0 0 0 0 1 0 0
  $ B: int  1431 2097 2712 24821 27359 41165 49221
  $ C: int  0 0 0 0 0 0 0
  $ D: chr  "7" "26.950000762939453" "57.95000076293945" "21" ...
  $ E: int  1 2 3 4 5 6 7

File Content:

 A  B   C   D   E
 0  1431    0   7   1
 0  2097    0   26.950000762939453  2
 0  2712    0   57.95000076293945   3
 0  24821   0   21  4
 1  27359   0   57.900001525878906  5
 0  41165   0   33.95000076293945   6
 0  49221   0   28.950000762939453  7

enter image description here

 > R.version
           _                           
 platform       x86_64-w64-mingw32          
 arch           x86_64                      
 os             mingw32                     
 system         x86_64, mingw32             
 status                                     
 major          3                           
 minor          1.0                         
 year           2014                        
 month          04                          
 day            10                          
 svn rev        65387                       
 language       R                           
 version.string R version 3.1.0 (2014-04-10)
 nickname       Spring Dance 
like image 973
user2105469 Avatar asked May 13 '14 15:05

user2105469


People also ask

Why is R reading my numbers as characters?

It's likely because they were originally factor s. You need as. numeric(as. character(........))

How do I change character to numeric in R?

To convert character to numeric in R, use the as. numeric() function. The as. numeric() is a built-in R function that creates or coerces objects of type “numeric”.

How do I set data to numeric in R?

Data Visualization using R Programming To convert columns of an R data frame from integer to numeric we can use lapply function. For example, if we have a data frame df that contains all integer columns then we can use the code lapply(df,as. numeric) to convert all of the columns data type into numeric data type.

How do I change character to numeric?

Convert character to numeric. To convert character values to numeric values, use the INPUT function. new_variable = input(original_variable, informat.); The informat tells SAS how to interpret the data in the original character variable.


1 Answers

This probably deserves a genuine answer that we can point to, so....

The behavior of type.convert was altered in R 3.1.0 (and, read below, will be largely reverted to its pre-3.1.0 behavior in R 3.1.1):

As from R 3.1.0, where converting inputs to numeric or complex would result in loss of accuracy they are returned as strings (for as.is = TRUE) or factors.

This created a fairly significant ruckus on the r-devel mailing list. The beginning of the relevant (and long) thread is here.

As Ben mentioned above, one of the results of that discussion is that the default behavior was restored in the development version for a subsequent release.

In the short term if you know which columns will be affected you can always use colClasses. Otherwise you'd have to modify your code to check the results of read.table and convert things yourself, I guess.

like image 96
joran Avatar answered Nov 16 '22 03:11

joran