So, I have a data frame, which I formed by combining lists of varying lengths by extending the shorter ones with NA
's.
Example:
[,1] [,2] [,3] [,4] [,5] [,6]
PAHS.147A "TNF" "TRAF6" NA NA NA NA
PAHS.122A "TRAF6" "TRIM25" NA NA NA NA
PAHS.012A "TRAF3" "TRAF4" NA NA NA NA
PAHS.3012A "CD40" "CD40LG" "CD5" "CD70" "CDKN1A" "CEBPB"
PAHS.038A "VEGFA" "VWF" NA NA NA NA
PAHS.084A "ULK2" "UVRAG" NA NA NA NA
I want to use write.table()
to write the table to a tab delimited text file, but instead of having NA
's, I just want to start a new line whenever a row reaches an NA
. Is there an easy way to do that?
I tried setting the NAs to "" but I still had tabs to indicate the missing values. I would just like a new line at that point instead of tabs.
Because write.table is designed to output regular objects (data.frames and matrices) with some sort of separator, you will probably get more traction with one of cat
, write
or writeLines
:
txt <- data.frame(a="a", b="b", c=NA, d=NA ,e=NA)
txt <- txt[rep(1,10),] # 10 copies
apply(txt,1,function(x) cat(x[!is.na(x)], "\n", file="out.txt", append=TRUE ))
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.