convert a range of numbers into a list?

62 views
Skip to first unread message

olivia solis

unread,
Nov 10, 2017, 1:51:21 PM11/10/17
to OpenRefine
Hello all,

I've been trying to figure this out, but need some help. I have a column full of ranges of numbers, e.g.:

row 1: 2-4
row 2: 7-12

I would like to be able to convert the cell so that it displays

row 1: 2,3,4
row 2: 7,8,9,10,11,12

My eventual goal being to split the cell with each number on a separate row in the record with Split multivalued cells. Is there a way to do this in OpenRefine?

Thank you!!
Olivia

John Little

unread,
Nov 10, 2017, 2:30:40 PM11/10/17
to openr...@googlegroups.com
Hi Olivia.

the forRange function in GREL (documentation) will work really well here.

Steps:
  1. split the range by the dash into two separate columns
  2. use forRange to split into a comma separated list
    • forRange(cells["range 1"].value -1, cells["range 2"].value, 1, v, v + 1).join(",")
  3. split multivalued cells
csrange_openRefine.gif

Hope that helps.

--John

--
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.
For more options, visit https://groups.google.com/d/optout.

olivia solis

unread,
Nov 10, 2017, 2:53:06 PM11/10/17
to OpenRefine
Thank you!! You've no idea how much time this will save me.

Thad Guidry

unread,
Nov 10, 2017, 3:08:37 PM11/10/17
to openr...@googlegroups.com
Olivia,

We'll make this even easier for you next time. :)

I've opened an new issue #1319 to add a new range() function for GREL.  Other languages like Python have this and we should also.

It will work like this...

can take accept a String form:
range("2-4") --> [2,3,4]

or a String form in a value
range(value) --> [2,3,4]

or 2 numbers
range(2, 4) --> [2,3,4]

and doing join(",") will convert the Array to a String
range(2,4).join(","). --> 2,3,4

olivia solis

unread,
Nov 10, 2017, 4:10:23 PM11/10/17
to OpenRefine
Wonderful!
Thank you.

Ettore Rizza

unread,
Nov 11, 2017, 4:55:21 PM11/11/17
to OpenRefine
For the sake of completeness, you can also perform the operation in Jython. But in this case, the method seems neither clearer nor faster than that proposed by John Little.

values= value.split('-')

list
= range(int(values[0]), int(values[1])+1)

return ', '.join(str(x) for x in list)


Reply all
Reply to author
Forward
0 new messages