Skip to content
Advertisement

What’s the R equivalent of SQL’s LIKE ‘description%’ statement?

Not sure how else to ask this but, I want to search for a term within several string elements. Here’s what my code looks like (but wrong):

des is a vector that stores strings such as “Swinging Strike”, “In play (run(s))”, “In play (out(s) recorded)” and etc. What I want inplay to store is a 1s and 0s vector corresponding with the des vector, with the 1s in inplay indicating that the des value had “In play%” in it and 0s otherwise.

I believe the 3rd line is incorrect, because all this does is return a vector of 0s with a 1 in the last element.

Thanks in advance!

Advertisement

Answer

The R analog to SQL’s LIKE is just R’s ordinary indexing syntax.

The ‘LIKE’ operator selects data rows from a table by matching string values in a specified column against a user-supplied pattern

In SQL, that would be:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement