andrekos
andrekos

Reputation: 2892

0-1 sequence without spaces

Spaces are redundant when reporting a binary sequence. This code

x <- '1 0 0 0 0 0 1 1 0 1 0 1 1 0 '
y<-gsub(' +', '', x)

does the job so I can copy and paste from R. How do I do the same for 0-1 sequences (and other one-digit data) in others formats, e.g.,

x <- c(1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0)

or

toString(x)

or whatever (for the sake of learning various options)? Thanks.

Upvotes: 3

Views: 221

Answers (2)

Federico Giorgi
Federico Giorgi

Reputation: 10765

Have you tried

write.table(x,row.names=FALSE,col.names=FALSE,eol="\t")
1   0   0   0   0   0   1   1   0   1   0   1   1   0   

By changing the eol (end of line) character, you can decide if and what separator to use.

Upvotes: 1

Sharpie
Sharpie

Reputation: 17693

For vectors, use the paste() function and specify the collapse argument:

x <- c(1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0)
paste( x, collapse = '' )

[1] "10000011010110"

Upvotes: 11

Related Questions