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.