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

R separate numbers and characters in a string

I have a string

rule <- "X[,16] <= -0.664 & X[,11] <= -2.1891 & X[,17] >= -0.4138"

I want to split a string into two parts

num_part = c(16 , -0.664 , 11 , -2.1891 , 17 , -0.4138)
char_part = c("X[,]<= & X[,] <= & X[,] >=")

I found a similar question but I couldn’t apply any of the eight examples to my data

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

>Solution :

We may use

library(stringr)
 scan(text = str_remove_all(rule, "\\[|\\]|\\>=|<=|&|X|,"), what = numeric(), quiet = TRUE)
[1] 16.0000 -0.6640 11.0000 -2.1891 17.0000 -0.4138
str_remove_all(rule, "[0-9.-]+")
[1] "X[,] <=  & X[,] <=  & X[,] >= "
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