简体   繁体   中英

How to remove trailing spaces in write.table in R

I have a lagre data frame which looks similar to this format:

line1
line2<tab>value1

When it is read in R using read.csv it is forced into a data frame as follows:

V1<tab>V2
line1<tab>NA
line2<tab>value1

I can replace the NA with an empty string, but when I write using write.table, I get a tab and empty space after line 1 in the output file.

How do I make it so that the output is in the same format as the input ie the trailing tabbed white space be removed

Sample file appended:

#Sample SGA file format
@HD VN:1.0.0    IA:NA
@PL NM:TEST
1   1   705 50947   YDL185W YOR202W -   -   -
1   2   377 50947   YDL185W YOR202W -   -   -
1   3   317 50947   YDL185W YOR202W -   -   -
...
@SP CF:ORF,IGNA
TEST    1
TEST2   1

head(dput(data))

structure(list(V1 = c("#Sample SGA file format", "@HD", 
"@PL", "1", "1", "1"), V2 = c("", "VN:1.0.0", "NM:TEST", "1", 
"2", "3"), V3 = c("", "IA:NA", "", "705", "377", "317"), V4 = c(NA, 
NA, NA, 50947L, 50947L, 50947L), V5 = c("", "", "", "YDL185W", 
"YDL185W", "YDL185W"), V6 = c("", "", "", "YOR202W", "YOR202W", 
"YOR202W"), V7 = c("", "", "", "-", "-", "-"), V8 = c("", "", 
"", "-", "-", "-"), V9 = c("", "", "", "-", "-", "-")), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9"), row.names = c(NA, 
6L), class = "data.frame")

and str(data)

'data.frame':   1541 obs. of  9 variables:
 $ V1: chr  "#Sample SGA file format" "@HD" "@PL" "1" ...
 $ V2: chr  "" "VN:1.0.0" "NM:TEST" "1" ...
 $ V3: chr  "" "IA:NA" "" "705" ...
 $ V4: int  NA NA NA 50947 50947 50947 50947 50947 50947 50947 ...
 $ V5: chr  "" "" "" "YDL185W" ...
 $ V6: chr  "" "" "" "YOR202W" ...
 $ V7: chr  "" "" "" "-" ...
 $ V8: chr  "" "" "" "-" ...
 $ V9: chr  "" "" "" "-" ...

I'll wager a guess. It sounds like you could do one of two things.

First, you could use

data[is.na(data)] <- ''
library(stringr)
write.table(str_trim(apply(data, 1, paste, collapse='\t')),
            'fileout.tsv',
            row.names=FALSE)

Or you can use a command line utility like sed to remove trailing whitespace from a file:

sed -e :a -e 's/^.\{1,77\}$/ & /;ta'

This is very convoluted, but here goes.

  1. Read line1 as a header in read.csv : foo <- read.csv("input.csv")

  2. Write just the 1st column name using write : write(colnames(foo)[1],"out/output.csv")

  3. Finally, write the rest of the table using append and without column names: write.table(foo,"output.csv",sep=",",row.names=F,col.names=F,append=T,quote=F)

This should get you the output file in the same format at the input file.

This is similar to Justin's answer, using regex.

cn <- file("output.txt",open="w") #opens write connection to file
writeLines(paste(names(data),collapse="\t"),con=cn) #writes header
#converts data frame into vector of character, with fields separated by tabs
to.print <- apply(data,1,paste,collapse="\t") 
to.print <- gsub("\\tNA$","",to.print) #deletes trailing <tab>NA
writeLines(to.print,con=cn) #writes data frame rows
close(cn)

If you want read.table to behave exactly as read.csv does, all you need to do is make the parameters the same

read.table(file, header = TRUE, sep = ",", quote="\"", dec=".",
     fill = TRUE, comment.char="")

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