如何使用 SQL/SAS 中等效的 "like" 运算符在 R 中进行条件替换

How to do conditional replacement in R using "like" operator equivalent in SQL/SAS

我的 data.frame 有超过 100 个观察。下面是它的快照

   Variable
   BloodPressure
   HeartRate
   Residual
   BloodPressure1
   HeartRate1
   Residual1
   BloodPressure2
   HeartRate2
   Residual2

我想去掉上面的数字部分 data.frame。我的新输出应该看起来像

         Variable
         BloodPressure
         HeartRate
         Residual
         BloodPressure
         HeartRate
         Residual
         BloodPressure
         HeartRate
         Residual

我是否可以像在 SAS 中那样在 R 中使用以下条件

 if variable like "%BloodPressure%" then new_variable = "BloodPressure"

或者我可以只从变量中提取字符部分并创建 new_variable 吗?

如果您只是想删除字符串中的数字,请尝试使用 R 函数 $ gsub $

> gsub("\d+","","residual1")
[1] "residual"