Reading valid (alpha) numeric fields in R

The tab delimited text file, which is actually an export (using bcp) of the database table, has this form (first 5 columns):

102 1   01  e113c   3224.96     12  
102 1   01  e185    101127.25   12
102 2   01  e185    176417.90   12
102A   3    01  e185    26261.03    12

I tried to import it into R using a command like

data <- read.delim("C:\\test.txt", header = FALSE, sep = "\t")

The problem is that the 3rd column, which is actually a varchar (alphanumeric) field, is erroneously read as an integer (since there are no letters in the entire column), and the leading zeros have disappeared. The same thing happened when I imported data directly from the database using odbcConnect. Again this column was read as a whole.

str(data)
$ code: int  1 1 1 1 1 1 6 1 1 8 ...

How can I import such a data set into R correctly so that I can safely populate this db table after some data manipulation?

EDIT

, read.delim

 colClasses = c("factor","integer","factor","factor","numeric","character","factor","factor","factor","factor","integer","character","factor")
  • "" "" varchar?

  • "" datetime?

  • , 540912.68999999994 , , 540912.69?

colClasses, , .

+3
2

"" "" varchar?

, . , . , factor. (, ), character .

"" datetime?

, , , Date POSIXct/POSIXlt.

, , 540912.68999999994, , 540912.69?

( 15 ); 540912.69 - , .

print(540912.68999999994)             # 540912.7
print(540912.68999999994, digits=22)  # 540912.69
print(540912.6899999994)              # 540912.7
print(540912.6899999994, digits=22)   # 540912.6899999994

EDIT: , Rmpfr.


colClasses, , .

colClasses ( ) , , . , 01 , , .

+4

- , . , . , . , ifelse() , . , - , , , .

, , read.delim read.table - , sep , .

+1

Source: https://habr.com/ru/post/1743306/


All Articles