[Rd] binary string conversion to a vector (PR#14120)
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Dec 9 12:23:55 CET 2009
On Wed, 9 Dec 2009, brglez at ncsu.edu wrote:
> Full_Name: Franc Brglez
> Version: R 2.9.1 GUI 1.28 Tiger build 32-bit (5444)
> OS: MacOSX -- 10.6.2
> Submission from: (NULL) (24.148.163.114)
>
>
> I am demonstrating what may be a bug or my lack of experience. Please review as
> it would help to hear from someone.
> MANY THANKS -- Franc Brglez
Please review the section 'BUGS' in the FAQ to work out why you have
just annoyed the developers who will have to clear up after you. You
were asked only to use R-bugs when you 'know for certain', and only to
report on current versions of R.
You started xvector as NULL: concatentating NULL and an object of
noquote dispatches on the first argument, NULL, and uses the default
method. So xvector correctly does not have class "noquote". A simple
way to do this would be
binS2binV <- function(string="0001101", sep="")
noquote(strsplit(string, sep)[[1]])
or replace noquote by as.integer.
Note too the cat() and paste() print things differently, so using
cat() to debug is not telling you anything useful about the eventual
output.
>
> The function "binS2binV" returns what I consider a wrong value -- see the
> terminal output
>
> binS2binV = function(string="0001101", sep="")
> # this procedure is expected to convert a binary string to a binary vector ...
> # but does it?? Why do we get a vector with quoted binary values??
> {
> qlist = strsplit(string, sep)
> qvector = qlist[[1]]
> cat("\n string=", string)
> cat("\n qvector=", qvector)
> n = length(qvector) ; xvector = NULL
> for (i in 1:n) {
> tmp = noquote(qvector[i])
> cat("\n", i,", tmp=", tmp)
> xvector = c(xvector, tmp)
> cat("\n", i,", xvector=", xvector)
> }
> cat("\n")
> return(xvector)
> }
>
>> print(binS2binV("10101100"))
>
> string= 10101100
> qvector= 1 0 1 0 1 1 0 0
> 1 , tmp= 1
> 1 , xvector= 1
> 2 , tmp= 0
> 2 , xvector= 1 0
> 3 , tmp= 1
> 3 , xvector= 1 0 1
> 4 , tmp= 0
> 4 , xvector= 1 0 1 0
> 5 , tmp= 1
> 5 , xvector= 1 0 1 0 1
> 6 , tmp= 1
> 6 , xvector= 1 0 1 0 1 1
> 7 , tmp= 0
> 7 , xvector= 1 0 1 0 1 1 0
> 8 , tmp= 0
> 8 , xvector= 1 0 1 0 1 1 0 0
> [1] "1" "0" "1" "0" "1" "1" "0" "0" <== what I want is "unquoted" binary
> values
>
> on the other hand, similar, NOT-THE-SAME but related, "conversions" are working,
> e.g.
> # seq = "1 0 0 1 0"
> #> seq
> # [1] "1 0 0 1 0"
> #> strsplit(paste(seq), " ")
> # [[1]]
> # [1] "1" "0" "0" "1" "0"
> #> paste(strsplit(paste(seq), " ")[[1]], collapse="")
> # [1] "10010"
> #
> #> seq=c(0,0,0,1,1,0,1)
> #> seq
> # [1] 0 0 0 1 1 0 1
> #> paste(strsplit(paste(seq), " "), collapse="")
> # [1] "0001101"
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel
mailing list