0

when I read a .csv file with read.csv like this

df1 <- read.csv("a.csv")

and I access a single column like this

df1[,1]

I get the expected column vector.


But in contrast, if i read the .csv with fread (from the library data.table)

df2 <- fread("a.csv")

and access a single column

df2[,1]

It just returns

1

Can somebody explain, why I can't access the column vector by its index, when I read the csv with fread?

Krantz
  • 1,424
  • 1
  • 12
  • 31
smudo78
  • 478
  • 1
  • 3
  • 13
  • 5
    Because you need to read the [tuorial](https://github.com/Rdatatable/data.table/wiki/Getting-started). In other words, `fread` in not some base R function rather coming from the `data.table` package (which you failed to mention) and creates a `data.table` objects which has it's own syntax. – David Arenburg Feb 21 '16 at 13:14
  • Okay, thanks for the answer. – smudo78 Feb 21 '16 at 13:24
  • 4
    In other words use `df2[, 2, with = FALSE]` or `df2[[1]]` or pass the (unquoted) column name instead the index. – David Arenburg Feb 21 '16 at 13:27

1 Answers1

1

According to the ?fread in data.table, you will find a parameter data.table:

data.table TRUE returns a data.table. FALSE returns a data.frame.

By default, data.table is TRUE, hence a data.table is created.

If you prefer the df2[,1] style, using:

df2 <- fread("a.csv",data.table=FALSE)
Sixiang.Hu
  • 1,009
  • 10
  • 21