Combining the if and contains GREL functions to add a new column based on existing columns

Ok, I have two columns in the same worksheet, Column A and column B. Column A has a single keyword and Column B has a longer abstract (all text based). I am hoping to create a new column giving me a true or false on whether or not Column B contains the text in the corresponding row of Column A. I tried to use the add column based on this column feature on Column A with this syntax: if(cells["Column B"].contains(value), "true", "false")

Unfortunately, this didn't work for me. I kept getting "false" for each row even if Column B contained text in Column A. I would also be happy if there was a way to do this with text facet or another OpenRefine feature, I just thought this would be the best way to try it.

If you add .value after the brackets, that expression will work:
if(cells["Column B"].value.contains(value), "true", "false")

However, you can write this more simply as:
value.contains(cells["Column B"].value)

This will generate a true/false in the new column. You can also use that expression directly in a custom text facet if you're simply exploring the data and don't need the condition recorded in a new column.

Hope that helps! There's more information in the documentation about using the cells object: Expressions | OpenRefine

The more simple form didn't work for me, but the longer expression worked great! Thanks so much for your help!

I think the simpler one should have been
cells["Column B"].value.contains(value)
when applied to Column A, and you can do this a custom facet.

2 Likes

Yes, that updated simple expression works as well, thanks for the follow-up!