列名在read.table或read.csv上向左移 [英] Column names shift to left on read.table or read.csv
本文介绍了列名在read.table或read.csv上向左移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最初我有这个TSV文件(示例):
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
所以我正在使用read.csv从.tsv文件中读取数据,但是我总是得到以下输出:
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
而不是得到这个:
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")
推荐答案
这是我必须解决的问题:将row.names设置为FALSE
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)
这篇关于列名在read.table或read.csv上向左移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文