Defining variable type before/while importing data

150 views Asked by At

I am importing several tab separated files into R. In one of the columns, there are numeric IDs which are 18+ digits long. When I use read.table for this, it automatically reads that column as numeric, converts the ID into scientific format (e.g. x.xxE10) and then when I use as.character on this, it results in the same string even if the original IDs were two different numbers.

Is there any way by which I can define in R how to read the data before reading the data? Or in general, how do I solve this problem?

I am simply using the read.table command

df <- read.table(file="data/myfile.txt",sep="\t",header=T, stringsAsFactors=F, encoding="UTF-8")
1

There are 1 answers

0
dsauce On

here is the solution. my file contains 20 columns

df <- read.table(file="data/myfile.txt",sep="\t",header=T, stringsAsFactors=F, encoding="UTF-8", colClasses=c(rep("character",20)))