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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With