简体   繁体   中英

write.table in R produces spaces in delimited

I'm writing a dataframe to be a .bed file; 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 . . . 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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