Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string into columns in R

Tags:

string

split

r

I have a string

mat <- c("[('value-66 > 0.03', 0.1314460), ('0.03 < value-222 <= 0.06', -0.062805), ('0.01 < value-93 <= 0.03', -0.058007), ('value-141 > 0.05', -0.051339234), ('value-108 <= 0.01', -0.0373), ('value-303 > 0.02', 0.037257)]") 

I want to divide each parentheses values into three columns.

For the first exemple the final matrix will contain three columns:

value-66, > 0.03, 0.1314460

My difficulty is with example like this one:

'0.01 < value-93 <= 0.03', -0.058007

I have not found a solution to put it into three columns like:

value-93, 0.01 <  <= 0.03, -0.058007

I have tried this but it didn't cut correctly

s <- strsplit(mat, ",")
s1 <- lapply(s, function(x) trimws(x,which=c('both')))
s1 <- lapply(s1, function(x) strsplit(x,' '))

Do I have to set conditions in a loop?

like image 285
Noura Avatar asked Jan 23 '26 15:01

Noura


1 Answers

You don't need a loop function.

Try this:

library(stringr)

mat <- c("[('value-66 > 0.03', 0.1314460), ('0.03 < value-222 <= 0.06', -0.062805), ('0.01 < value-93 <= 0.03', -0.058007), ('value-141 > 0.05', -0.051339234), ('value-108 <= 0.01', -0.0373), ('value-303 > 0.02', 0.037257)]") 

mat %>%
  str_extract_all("\\(.+?\\)") %>%
  sapply(str_remove_all, "\\(|\\)|\\'") %>%
  as.character() %>%
  str_split(",") %>%
  (
    function(i){
      c12 <- sapply(i, "[[", 1)
      c1 <- str_extract(c12, "value[^ ]+")
      c2 <- str_remove(c12, c1)
      c3 <- sapply(i, "[[", 2)
      cbind(c1, c2, c3)
    }
  )
     c1          c2                c3             
[1,] "value-66"  " > 0.03"         " 0.1314460"   
[2,] "value-222" "0.03 <  <= 0.06" " -0.062805"   
[3,] "value-93"  "0.01 <  <= 0.03" " -0.058007"   
[4,] "value-141" " > 0.05"         " -0.051339234"
[5,] "value-108" " <= 0.01"        " -0.0373"     
[6,] "value-303" " > 0.02"         " 0.037257"  

stringr is my favorite for doing string manipulation including regex. It is consistent and the functions are easier to remember. However, you can use R base function to doing it, if you want.

like image 143
nurandi Avatar answered Jan 26 '26 08:01

nurandi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!