Chayma Atallah
Chayma Atallah

Reputation: 766

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")

Upvotes: 4

Views: 6944

Answers (2)

Chayma Atallah
Chayma Atallah

Reputation: 766

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)

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

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])

Upvotes: 3

Related Questions