Anshul Gautam Anshul Gautam - 1 month ago 16
R Question

String operations in R

Suppose I have a string in this format in R:

"A<2 & A<=3 & B>5 & B<=1 & A<1"


Now I hope to get the four variables i.e. 'A_less'(A<) , 'A_greater'(A>) , 'B_less'(B<) and 'B_greater'(B>) that contatin their respective values i.e.

in case of 'A<' compare all values for that particular substring and give the best value for that condition i.e

'A_less' = 1
'B_less' = 1
'B_greater' = 5
'A_greater' = NA


another example:

string <- c("A>3 & A<9 & A<=-1 & A>2 & B<=5")


then the output of the variable would be

'A_less' = -1
'A_greater' = 3
'B_less' = 5
'B_greater' = NA


Thanks!

Answer

This should work for you:

library(stringr)
x="A<2 & A<=3 & B>5 & B<=1 & A<1"
A_less=min(as.numeric(str_match_all(x,'(?:A<=?)(-?[0-9]+)')[[1]][,2]))
B_less=min(as.numeric(str_match_all(x,'(?:B<=?)([-?0-9]+)')[[1]][,2]))
A_greater=max(as.numeric(str_match_all(x,'(?:A>=?)(-?[0-9]+)')[[1]][,2]))
B_greater=max(as.numeric(str_match_all(x,'(?:B>=?)(-?[0-9]+)')[[1]][,2]))
A_less=ifelse(A_less==-Inf,NA,A_less)
B_less=ifelse(A_less==-Inf,NA,B_less)
A_greater=ifelse(A_greater==-Inf,NA,A_greater)
B_greater=ifelse(B_greater==-Inf,NA,B_greater)