简体   繁体   English

R 中的 write.table 产生分隔的空格

[英]write.table in R produces spaces in delimited

I'm writing a dataframe to be a .bed file;我正在编写一个数据框作为 .bed 文件; the data frame looks fine but when I export it using:数据框看起来不错,但是当我使用以下方法导出它时:

write.table(x = bed_file, 
              file = 'merged_out.bed', 
              row.names = F, 
              col.names = F, 
              quote = F, 
              sep = '\t')

and then view the head of merged.bed in terminal some rows are space-delimitated but most are tab delimitated .然后在终端中查看merged.bed 的头部,一些行是空格分隔的,但大多数是制表符分隔的。 . . . . any ideas?有任何想法吗?

chr1    3816670 3818113 181 4 
chr1    6452977 6454435 181 1 
chr1    8075042 8075406 181 5 
chr1    8389451 8389713 181 1 
chr1    11190170    11190527    181 1 
chr1    14454661    14454861    181 2 
chr1    16212079    16213143    181 2 

I suspect that those are not "spaces" but just blank space that are actually tabs.我怀疑那些不是“空格”,而只是实际上是制表符的空格。

To verify:验证:

### the first row
readLines("merged_out.bed", n = 1)
# [1] "chr1\t3816670\t3818113\t181\t4"

### the first seemingly-wrong line
readLines("merged_out.bed", skip = 4, n = 1)
# [1] "chr1\t3816670\t3818113\t181\t4"

It is clear that there are no " " characters in those two lines.很明显,这两行中没有" "字符。

If you want to see if there are any literal spaces anywhere in the file, this expression will verify the entire file (assuming that you have no legitimate spaces in the fields):如果您想查看文件中是否有任何文字空格,此表达式将验证整个文件(假设字段中没有合法空格):

any(grepl(" ", readLines("merged_out.bed")))
# [1] FALSE

Data prep:数据准备:

dat <- read.table(text = "
chr1    3816670 3818113 181 4 
chr1    6452977 6454435 181 1 
chr1    8075042 8075406 181 5 
chr1    8389451 8389713 181 1 
chr1    11190170    11190527    181 1 
chr1    14454661    14454861    181 2 
chr1    16212079    16213143    181 2 ")
write.table(dat, "merged_out.bed",
            row.names = FALSE, col.names = FALSE, quote = FALSE, sep = "\t")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM