Avi
Avi

Reputation: 2283

Extra Line Feed in end of file in writeline

I have the following data frame (which is also data table):

data6

1 44348616 1 1 OV1
2 44348616 2 1 OV2
3 44348616 3 1 OV3
4 44348695 1 1 OV4
5 44348695 2 1 OV5
6 44348695 3 1 OV6
7 44348496 1 1 OV7
8 44248192 1 1 OV8
9 44348697 1 1 OV9

I would like to write it to a file as-is. I use the following commands:

s = paste(apply(t(data6), 2, paste, collapse=' '),collapse='',"\n")
writeLines(s, con=file("C:/MyLittleData.txt"))

When I look at the file MyLittleData.txt with an editor I see 2 extra lines. The last line indicates the end of the file (which is good) and before it there is an additional extra empty line. How can I avoid it?

Upvotes: 0

Views: 52

Answers (1)

csgillespie
csgillespie

Reputation: 60472

Everything seems fine to me:

## Read in the data
data6 = read.table(textConnection("1 44348616 1 1 OV1
2 44348616 2 1 OV2
3 44348616 3 1 OV3
4 44348695 1 1 OV4
5 44348695 2 1 OV5
6 44348695 3 1 OV6
7 44348496 1 1 OV7
8 44248192 1 1 OV8
9 44348697 1 1 OV9"))
R> dim(data6)
[1] 9 5

Run your code:

s = paste(apply(t(data6), 2, paste, collapse=' '),collapse='',"\n")
writeLines(s, con=file("/tmp//MyLittleData.txt"))

Read in your data set and count the lines:

length(readLines("/tmp//MyLittleData.txt"))
[1] 10

This is more a comment than an answer, but it's too large for comment

Upvotes: 1

Related Questions