简体   繁体   中英

R write.table append dataframe to existing file

I'm writing in output file a line (str) then a dataframe. But dataframe overwrite the first line even with append=TRUE.

rgb_file <- file("/Path/output/rgb.bed")
header_rgb="track\ttype=bed\tname=merged_replicates_paste_pPGK_input\titemRgb=on\n"
write(header_rgb, rgb_file, sep = '')
write.table(df, rgb_file, quote = FALSE, row.names = FALSE,col.names = FALSE,sep = "\t",append = T)

File after writing the first line:

head rgb.bed 
track   type=bed    name=merged_replicates_paste_pPGK_input itemRgb=on

File after writing the df with append = TRUE

head rgb.bed 
chr1    4457336 4457386 region_1    1   +   4457336 4457386 1,0,0
chr1    4457386 4457436 region_1    1   +   4457386 4457436 0,26,0
chr1    4457436 4457486 region_1    1   +   4457436 4457486 0,25,0
chr1    4457486 4457536 region_1    1   +   4457486 4457536 0,35,0

Am I doing something wrong? Is there other way to write a first line then append a datafrmae to the file?

This solved the problem. It prints the first character line and then the dataframe

cat(header_rgb, file ='/path/output/rgb.bed')
fwrite(x = df,
       file = '/path/output/rgb.bed',
       sep = "\t",
       col.names=F,
       append=T)

I let it here if someone need it.

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