[Rd] binary string conversion to a vector (PR#14120)

brglez at ncsu.edu brglez at ncsu.edu
Wed Dec 9 05:35:12 CET 2009


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

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"



More information about the R-devel mailing list