How to quickly filter columns based on their names in DolphinDB?

12 Views Asked by At

Supposing there are column names in the form of “event_i_j“ (“i“ and “j” represent numbers), such as “event_1_5“ and “event_5_1“. How to quickly select those with i > j?

Take the table t generated with the following script as an example:

t = table(1 2 3 4 as event_1_5, 1 3 4 5 as event_2_5, 1 2 3 4 as event_5_1, 1 2 3 4 as event_5_8, 1 2 3 4 as event_6_3)

How to select “event_5_1“ and “event_6_3“ from t?

1

There are 1 best solutions below

0
dbaa9948 On

You can use the split function to split each column name into “event“, “i“, and “j“. Compare “i“ and “j“ and select those with i > j. Here is an example SQL script generated with metaprogramming:

t = table(1 2 3 4 as event_1_5, 1 3 4 5 as event_2_5, 1 2 3 4 as event_5_1, 1 2 3 4 as event_5_8, 1 2 3 4 as event_6_3)
colNames=t.columnNames()
s=split(colNames,"_")
cols=colNames[each(x->int(x[1])>int(x[2]), s)]
sql(select=sqlCol(cols), from=t).eval()