Sim101011
Sim101011

Reputation: 305

Use a string as a condition in If- else

I have a variable which is count_process = "time>=20" Now i want to use it in a IF condition like

if(time>=20){ do something }

How can I do that?

Upvotes: 1

Views: 2107

Answers (1)

akrun
akrun

Reputation: 886998

One option would be to extract the numeric substring with sub and use it in the if condition

 val <- as.numeric(sub('[^0-9]+', '', count_process))
 if(time >= val){do something}

Other option would be using eval(parse( (not recommended though)

 if(eval(parse(text=count_process))){do something}

Upvotes: 3

Related Questions