Simple OpenRefine IF to create a new column
Asked Answered
D

2

7

Im trying to create a new column which contains true or false. Basically column A has a number in it, between 1 and 6, if its higher than 3 I want the new column 'match' to contain true, otherwise it contains false. Using the add column based on column in trying the following GREL

if(value > 5, "True", "False")

That basically results in EVERYTHING being false.

I know my IF statement is correct because the following works

if(value.length() > 1, "Double", "Single")

Im just confused why if Value is greater than 5 doesnt work, its obviously missing something but I cant seem to pinpoint it in the docs.

Darky answered 13/10, 2016 at 16:57 Comment(1)
Are the values in the column formatted as numbers and not as strings?Orator
P
8

Your GREL if() is correct. Our docs for that are here: https://github.com/OpenRefine/OpenRefine/wiki/GREL-Controls

But I wonder if you really have all number values in that Column ? Are all the values "green" color ? If not, try using Edit Column to Trim Whitespace and then convert the Text to Numbers. Then try your if() on that column again and see what happens.

Phenology answered 14/10, 2016 at 2:43 Comment(2)
Alternatively you could modify the GREL to include this step (leaving the original column as a string) - if(value.toNumber() > 5, "True", "False")Cowpuncher
ahh, I just presumed as it was a number it was treated as numeric, but it was being treated as a string. Used edit cells to trim whitespace (no cells changed), then used the common transformation 'to number'. Data was imported from a csv out of a database where it was numeric. Now my numers are 'green' and the equation works. Thanks.Darky
I
1

if(value.toNumber() > 5, "True", "False")

Inconvenience answered 8/9, 2023 at 2:56 Comment(1)
Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Morelock

© 2022 - 2024 — McMap. All rights reserved.