Rosa
Rosa

Reputation: 1833

R read data with 0 in front

How can I read data with 0 in front, eg:

read.table(header=T, stringsAsFactors=F,text="
a b
1 2
3 04
")

I got the second row of 3, 4 instead of 3, 04, what should I do to keep the 0 in front, thanks.

Upvotes: 0

Views: 210

Answers (1)

Josh O'Brien
Josh O'Brien

Reputation: 162421

You can use read.table()'s colClasses= argument to let it know that you want to read the columns in as vectors of class "character":

read.table(header=T, stringsAsFactors=F,
colClasses="character", 
text="
a b
1 2
3 04
")
#   a  b
# 1 1  2
# 2 3 04

(Or, to read your first column as numeric and only the second one as character, set colClasses=c("numeric", "character"))

Upvotes: 3

Related Questions