[英]Column names shift to left on read.table or read.csv
Originally I have this TSV file (sample): 最初我有这个TSV文件(示例):
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: 所以我正在使用read.csv从.tsv文件读取数据,但是我总是得到以下输出:
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")
There appears to be a missing column header in your source CSV file. 源CSV文件中似乎缺少列标题。 One option here would be to leave your
read.csv()
call as it is and simply adjust the names of the resulting data frame: 这里的一种选择是保持您的
read.csv()
调用read.csv()
,只需调整结果数据框的名称即可:
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])
这是我必须解决的问题:将row.names设置为FALSE
write.table(dataset, file = "data.tsv", sep = "\t", row.names = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.