0-1 without spaces

Spaces are redundant when representing 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 to do the same for sequences 0-1 (and other single-bit data) in other formats, for example,

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 exploring various options)? Thanks.

+3
source share
2 answers

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

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

[1] "10000011010110"
+11
source

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 character (end of line), you can decide whether to use which separator to use.

+1
source

Source: https://habr.com/ru/post/1722965/


All Articles