1

I have a file "data.csv" with a few columns:

col1, col2, col3
001,  012,  0234

All three columns have strings like the data displayed, but when I run df = read.csv("data.csv"), View(df) looks like this.

col1, col2, col3
1,    12,   234

How do import the data to look the way it does in the CSV?

Username
  • 3,463
  • 11
  • 68
  • 111

1 Answers1

6

See colClasses in ?read.csv:

df = read.csv("data.csv", colClasses="character")

colClasses: character. A vector of classes to be assumed for the columns. If unnamed, recycled as necessary. If named, names are matched with unspecified values being taken to be ‘NA’.

Possible values are ‘NA’ (the default, when ‘type.convert’ is used), ‘"NULL"’ (when the column is skipped), one of the atomic vector classes (logical, integer, numeric, complex, character, raw), or ‘"factor"’, ‘"Date"’ or ‘"POSIXct"’.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453