Condition statement

1,518 views
Skip to first unread message

Dawayne eason

unread,
Jan 26, 2021, 12:49:57 AM1/26/21
to OpenRefine
I have been tasked to create a new column" has_offer" with the values 0 or 1 based on the "item_description" column with the following conditions: If it contains the text “discount” or “offer” or “sale”, then set the value in has_offer as 1, else 0.  

I'm trying to write an expression in GREL which will do this for multiple strings.

I used the below example, but it only works for one string and not multiple ones.
if(value.contains("test"),"Test data",value)

Isao Matsunami

unread,
Jan 26, 2021, 1:39:17 AM1/26/21
to openr...@googlegroups.com
How about this

if( length(value.find(/discount|offer|sale/)) > 0, 1, 0)



2021年1月26日(火) 14:49 'Dawayne eason' via OpenRefine <openr...@googlegroups.com>:
--
You received this message because you are subscribed to the Google Groups "OpenRefine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openrefine+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openrefine/dddb34a6-0ae4-40c3-9bfa-d1a6340062b3n%40googlegroups.com.
--
*************************************************
中日新聞電子編集部
松波 功 Isao Matsunami

451-0043 名古屋市西区新道1-2-18-908
mobile: +81-90-3954-5786
mail: isa...@on.rim.or.jp  PGP:1DF1 4682
*************************************************

Dawayne eason

unread,
Jan 26, 2021, 8:49:21 AM1/26/21
to OpenRefine
Thanks as this work.

Santhoshi Iyer

unread,
Mar 4, 2021, 2:10:34 PM3/4/21
to OpenRefine
This works ... 
if(value.toLowercase().contains("discount").or (value.toLowercase().contains("offer").or (value.toLowercase().contains("sale"))),1,0)  

Radhey Patel

unread,
Mar 8, 2021, 4:13:00 PM3/8/21
to OpenRefine

f the price is greater than 100, “high_priced” should be set as 1, else 0. Provide the GREL expression used to perform this.

Owen Stephens

unread,
Mar 9, 2021, 7:34:16 AM3/9/21
to OpenRefine
Hi Radhey and welcome

When posting questions here it's great if you can provide as much information as possible and if you can use a dedicated thread for a question rather than adding on the bottom of an existing thread.
However, in this case I think the thread already contains several examples of using a condition statement, and this is also well documented in the users manual - please have a look at https://docs.openrefine.org/manual/expressions#ife-etrue-efalse and if you have more specific questions about achieving the desired outcome in your particular case feel free to come back and ask questions in a new thread on this forum.

Best wishes

Owen

Anuradha Perumalsamy

unread,
Feb 9, 2022, 8:14:41 PM2/9/22
to OpenRefine
if(or(value.contains("discount"), value.contains("offer"), value.contains("sale")) , "1", "0") - it works
Reply all
Reply to author
Forward
0 new messages