Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column names shift to left on read.table or read.csv

Originally I have this TSV file (sample):

name   type   qty   
cxfm   1C     0
d2     H50    2
g3g    1G     2
hb     E37    1
nlx    E45    4

so I am using read.csv to read data from a .tsv file but I always get this output:

name   type   qty   
1      cxfm   1C     0
2      d2     H50    2
3      g3g    1G     2
4      hb     E37    1
5      nlx    E45    4

instead of getting this one:

       name   type   qty   
1      cxfm   1C     0
2      d2     H50    2
3      g3g    1G     2
4      hb     E37    1
5      nlx    E45    4

Any ideas this? this is what I am using to read the files:

    file_list<-list.files()

for (file in file_list){

  if (!exists("dataset")){
    dataset <- read.table(file, header = TRUE, sep = "\t", row.names = NULL, blank.lines.skip = TRUE, fill = TRUE)
    names(dataset) <- c("rowID", names(dataset)[1:ncol(dataset)-1])
    }

  if (exists("dataset")){
    temp_dataset <- read.table(file, header = TRUE, sep = "\t", row.names = NULL, blank.lines.skip = TRUE, fill = TRUE)
    names(temp_dataset) <- c("rowID", names(temp_dataset)[1:ncol(temp_dataset)-1])
    dataset <- rbind(dataset, temp_dataset)
    rm(temp_dataset)
  }

}

dataset <- unique(dataset)

write.table(dataset, file = "dataset.tsv", sep = "\t")
like image 442
Chayma Atallah Avatar asked Oct 16 '25 17:10

Chayma Atallah


2 Answers

There appears to be a missing column header in your source CSV file. One option here would be to leave your read.csv() call as it is and simply adjust the names of the resulting data frame:

df <- read.csv(file,
               header = TRUE,
               sep = "\t",
               row.names = NULL,
               blank.lines.skip = TRUE,
               fill = TRUE,
               comment.char = "",
               quote = "", stringsAsFactors = FALSE)

names(df) <- c("rowID", names(df)[1:ncol(df)-1])
like image 102
Tim Biegeleisen Avatar answered Oct 18 '25 12:10

Tim Biegeleisen


This is what I had to do to Fix it: set row.names to FALSE

write.table(dataset, file = "data.tsv", sep = "\t", row.names = FALSE)
like image 21
Chayma Atallah Avatar answered Oct 18 '25 12:10

Chayma Atallah