How to preserve zeroes in a numeric value when reading into R

Viewed 37

I have a tab delimited text file:

0730000    John    1    01    225    000    000

and when i read it into R

stud_stats <- read.delim("student.txt", header=FALSE, sep="\t", fileEncoding="UTF-8")

and viewed the dataframe, the value that is displayed is:

730000|John|1|1|225|0|0

The numeric values with zeroes in the initial text file have been altered in the dataframe. How do i preserve the numeric value to retain the zeroes when reading into the R dataframe?

I want the following exact value in the dataframe, just like the text file.

0730000|John|1|01|225|000|000   
1 Answers

You can specify the column values, for example by using data.table::fread:

> data.table::fread('book1.txt', colClasses = rep('character', 7))
     col1 col2 col3 col4 col5 col6 col7
1: 073000 John    1   01  225  000  000

Or by using base R (but fread is better),

read.table('book1.txt', colClasses = rep('character', 7), header = TRUE)
Related