mr.T
mr.T

Reputation: 634

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

Upvotes: 4

Views: 330

Answers (4)

ThomasIsCoding
ThomasIsCoding

Reputation: 101149

A base R option using regmatches (for num_part) and gsub (for char_part) with a pattern pat like below

> pat <- "-?(\\d+)?\\.?\\d+"

> as.numeric(unlist(regmatches(rule, gregexpr(pat, rule))))
[1] 16.0000 -0.6640 11.0000 -2.1891 17.0000 -0.4138

> gsub(pat, "", rule)
[1] "X[,] <=  & X[,] <=  & X[,] >= "

Upvotes: 2

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

You can do this in one go:

library(tidyverse)
data.frame(rule) %>%
  mutate(num = str_extract_all(rule, "[\\d.-]+"),
         char = str_extract_all(rule, "[^\\d.-]+"))
                                                      rule                                  num
1 X[,16] <= -0.664 & X[,11] <= -2.1891 & X[,17] >= -0.4138 16, -0.664, 11, -2.1891, 17, -0.4138
                                      char
1 X[,, ] <= ,  & X[,, ] <= ,  & X[,, ] >= 

Upvotes: 3

Colin Daglish
Colin Daglish

Reputation: 67

library(stringr)

rule <- "X[,16] <= -0.664 & X[,11] <= -2.1891 & X[,17] >= -0.4138"
num_part <- stringr::str_extract_all(rule, pattern = "[-+]?[0-9]\\d*(\\.\\d+)?")
char_part <- stringr::str_remove_all(rule, "[0-9.-]+")

Upvotes: 2

akrun
akrun

Reputation: 886978

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[,] >= "

Upvotes: 3

Related Questions