简体   繁体   中英

LIKE operator in R (on Databricks)

Here is a simple update, which works perfectly:

reg$region <- ifelse( reg$region %in% ('xxx'), 'yyy', reg$region)

How do I change this so it updates the region column when it CONTAINS given string? The above works for the exact match.

In SQL it would be like this:

UPDATE reg SET region = case when region like '%xxx%' then 'yyy' else region end;

Any help much appreciated.

Thanks Alex, this really helped - managed to find the solution. The WHEN function can use CONTAINS function which I thought was impossible to be used with IFELSE. Then I realized it can be used in a different way - without those %...% operators.

So it works now when used as below:

reg$region <- ifelse( contains( reg$region, 'xxx'), 'yyy', reg$region)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM