Hi to all I would to determinate whether bits is a binary code and I would to find out the which bit is set to 1 bits <-"00110110" I found to detect whether there are only numbers all.digits(bits) but is there any function to detect whether there are only 0 and 1 in the string And how could I get the f.e the third "bit" from the right hand side With regards Carmen
Carmen Meier wrote:> Hi to all > I would to determinate whether bits is a binary code and I would to find > out the which bit is set to 1 > > bits <-"00110110" > I found to detect whether there are only numbers > all.digits(bits) > but is there any function to detect whether there are only 0 and 1 in > the string > > And how could I get the f.e the third "bit" from the right hand side > > With regards Carmen >Hi, R> nc <- nchar(bits) R> substring(bits,1:nc,1:nc) == "1" R> which( substring(bits,1:nc,1:nc) == "1" ) Cheers, Romain -- Mango Solutions Tel +44 1249 467 467 Fax +44 1249 467 468 Mob +44 7813 526 123 data analysis that delivers
Try:
# TRUE if all 0 and 1
regexpr("^[01]*$", bits) > 0
# positions of 1s
gregexpr("1", bits)[[1]]
On 11/29/06, Carmen Meier <carmei3 at web.de>
wrote:> Hi to all
> I would to determinate whether bits is a binary code and I would to find
> out the which bit is set to 1
>
> bits <-"00110110"
> I found to detect whether there are only numbers
> all.digits(bits)
> but is there any function to detect whether there are only 0 and 1 in
> the string
>
> And how could I get the f.e the third "bit" from the right hand
side
>
> With regards Carmen
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Dear Carmen,
length(grep("[^01]", bits)) == 0
should do the trick, returning TRUE if the string contains only 0's and
1's.
I hope this helps,
John
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
--------------------------------
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Carmen Meier
> Sent: Wednesday, November 29, 2006 9:06 AM
> To: R-help
> Subject: [R] String question
>
> Hi to all
> I would to determinate whether bits is a binary code and I
> would to find out the which bit is set to 1
>
> bits <-"00110110"
> I found to detect whether there are only numbers
> all.digits(bits)
> but is there any function to detect whether there are only 0
> and 1 in the string
>
> And how could I get the f.e the third "bit" from the right hand
side
>
> With regards Carmen
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hi
gregexpr can be used
bits <-"0011011aaa0"
gregexpr("[01]", bits)
[[1]]
[1] 1 2 3 4 5 6 7 11
attr(,"match.length")
[1] 1 1 1 1 1 1 1 1
or
grep("[^01]", bits)
if you want to know that there is any other character then 0 or 1
gregexpr("1", bits)
if you want to know location of 1's
HTH
Petr
On 29 Nov 2006 at 15:06, Carmen Meier wrote:
Date sent: Wed, 29 Nov 2006 15:06:08 +0100
From: Carmen Meier <carmei3 at web.de>
To: R-help <r-help at stat.math.ethz.ch>
Subject: [R] String question
> Hi to all
> I would to determinate whether bits is a binary code and I would to
> find out the which bit is set to 1
>
> bits <-"00110110"
> I found to detect whether there are only numbers
> all.digits(bits)
> but is there any function to detect whether there are only 0 and 1 in
> the string
>
> And how could I get the f.e the third "bit" from the right hand
side
>
> With regards Carmen
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented,
> minimal, self-contained, reproducible code.
Petr Pikal
petr.pikal at precheza.cz