1

ENV:
R version 3.3.1
MAC OS X 10.9.4

I have data in file like:

V1 | V2 | V3 
------ | ------ | ------
1   | 2 | 3
0.5 | 0.3 | 0.2

But when I use read.table to import the data and print it to the screen, it looks like:

V1 | V2 | V3 
------ | ------ | ------
1.000000   | 2.000000 | 3.0000
0.500000 | 0.300000 | 0.20000

Values in the first row changes from integer to float.

Expected:

V1 | V2 | V3 
------ | ------ | ------
1   | 2 | 3
0.500000 | 0.300000 | 0.20000

There is colClasses in read.table but there is no rowClasses to specific data type of rows.

Is it possible to specific rows data using read.table? I did not find any solution yet. Thanks. Any references or alternative ways would be appreciated.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Nick Dong
  • 3,638
  • 8
  • 47
  • 84
  • 5
    It is not possible. `read.table` results in a `data.frame` which is (usually) a list with vectors of equal length. Each element of a vector must be of the same type as every other element. In you case, the first row elements must be converted to numeric in order to correspond with elements in subsequent rows. – lmo Jan 19 '17 at 16:25
  • 5
    read.table, then transpose using `t()`, then we can set column classes. – zx8754 Jan 19 '17 at 16:27
  • Does t() take a long running time? markdown tables in stackoverflow seems not work. @zx8754 – Nick Dong Jan 19 '17 at 16:31
  • `t()` transposes the data.frame. – CCurtis Jan 19 '17 at 16:32
  • Does t() take a long running time for big data? – Nick Dong Jan 19 '17 at 16:33
  • Depends on the size of data.frame. Regarding markdown: read about [R reproducible example](http://stackoverflow.com/questions/5963269) – zx8754 Jan 19 '17 at 16:34
  • 2
    If you're very concerned about efficiency, maybe consider `data.table::setDF(data.table::transpose(x))` instead, which will return a DF (while `t()` will give a matrix that you'll have to convert back to a DF). I doubt it will make a practical difference though. – Frank Jan 19 '17 at 16:53
  • Comments from Imo, zx8754, Frank together could be perfect answer for my current question. Thanks. – Nick Dong Jan 19 '17 at 17:06

0 Answers0