3

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   
2
  • 3
    You can define the read column as character to do so. E.g. you can write something like: data <- read.table("student.txt", colClasses=c("character", "character", "character", "character", "character", "character", "character"), sep = "\t", ...) Commented Jan 11, 2022 at 8:18
  • 1
    One approach could be to use the readr package function read_tsv("student.txt", col_names = F). Commented Jan 11, 2022 at 8:24

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.