1
myInput <- data.frame('X' = c(1, 2, 3, 4, 5), 
                      'n' = c(87, 119, 94, 95, 134),
                      'r' = c(76, 8, 74, 11, 0))

write.csv(myInput, file = "test.csv", row.names = FALSE)

input_file <- "test.csv"

#Load input
myInput <- read.csv(input_file, stringsAsFactors = FALSE)
a_csv <- myInput[-3]
b_csv <- myInput[,-3] 

Gives this:

> print(dim(a_csv))
[1] 5 2
> print(dim(b_csv))
[1] 5 2

Compared to this result with fread():

myInput <- fread(input_file, stringsAsFactors = FALSE)
a_fread <- myInput[-3] 
b_fread <- myInput[,-3]

> print(dim(a_fread))
[1] 4 3
> print(dim(b_fread))
[1] 5 2

So reading in data using these 2 methods return objects of the same type but indexing on them is giving different results. Why? And how can I make these consistent so that users who choose to use read.csv() don't get different results from users who choose fread()?

P.S. This is the closest I could find: read.csv and fread produce different results for the same data frame

But it has to do with how the data is being read. I couldn’t find anything that addresses the indexing issue.

warship
  • 2,924
  • 6
  • 39
  • 65

1 Answers1

0

read.csv() returns a data.frame. When you do myInput[-3] for this data.frame, the data.frame is treated as a list, and the indexing drops the third element of the list, i.e, the third column.

myInput <- read.csv(input_file, stringsAsFactors = FALSE)
class(myInput)
# [1] "data.frame"

fread() returns a data.table. When you do myInput[-3], this drops the third row of the data.table.

myInput <- fread(input_file, stringsAsFactors = FALSE)
class(myInput)
# [1] "data.table" "data.frame"

This is just the way data.table and data.frame are difference. More technically, it's a difference between [.data.table and [.data.frame.

Example:

DT <- data.table(a = 1:4, b = letters[1:4], c = LETTERS[1:4])
DT
#    a b c
# 1: 1 a A
# 2: 2 b B
# 3: 3 c C
# 4: 4 d D
DT[-3]
#    a b c
# 1: 1 a A
# 2: 2 b B
# 3: 4 d D
df <- data.frame(a = 1:4, b = letters[1:4], c = LETTERS[1:4])
df
#   a b c
# 1 1 a A
# 2 2 b B
# 3 3 c C
# 4 4 d D
df[-3]
#   a b
# 1 1 a
# 2 2 b
# 3 3 c
# 4 4 d
Drumy
  • 450
  • 2
  • 16