在 If-else 中使用字符串作为条件

Use a string as a condition in If- else

我有一个变量是 count_process = "time>=20" 现在我想在

这样的 IF 条件下使用它
if(time>=20){ do something }

我该怎么做?

一个选项是使用 sub 提取数字子串并在 if 条件

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

其他选项将使用 eval(parse((但不推荐)

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