Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get string '1,2,3' not '1:3'

There is a string: s <- '[[1,2,3],[2,5,6]]'

I use package jsonlite to:

s <- fromJSON(s) 
s <- split(s, row(s))

Then, I get a matrix like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A matrix: 2 × 3 of type int
1 2 3
2 5 6

I hope I can use as.character(s[1]) to make s[1] become '1,2,3'. However I get this '1:3'

as.character(s[1])
'1:3'

What should I do?

>Solution :

A possible solution:

s <- matrix(1:6,2,3, byrow=T)
s

#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    4    5    6

paste(s[1,], collapse=",")

#> [1] "1,2,3"

Without conversion to matrix:

library(stringr)

s <- '[[1,2,3],[2,5,6]]'

str_extract(s, "(?<=^\\[\\[).*(?=\\],)")

#> [1] "1,2,3"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading