Deep-Learner
Deep-Learner

Reputation: 119

Convert String to numeric vector

I have a function which takes a vector of zeros and ones for example:

foo(c(1,1,1,0,1,1))

But in some cases, there are more then 100 numbers and I have to add all commas manually. Is there any predefined function to convert a string to such a vector? Something like this:

foo(unknown_function("111011"))

Upvotes: 1

Views: 4055

Answers (1)

akrun
akrun

Reputation: 887831

We can use paste

foo <- function(vec){
   paste(vec, collapse="")
  }

foo(c(1,1,1,0,1,1))
#[1] "111011"

If we need to do the reverse

foo1 <- function(str1){
      as.integer(unlist(strsplit(str1, "")))
  }
res <- foo1("111011")
res
#[1] 1 1 1 0 1 1

Or may be the OP meant

dput(res)

Upvotes: 3

Related Questions