Select first n columns of a table

Hello all,
I’m not very familiar with KNIME - mostly I use Mathematica… Now I face a “problem” which should not be one. I simply want to get the first n (say 50) columns from a larger table. I tried with a table creator and appended a counter (from 1 to 50) the transposing and tried to use the reference column filter, but this did not work. Can anyone give me a hint?

Greetings from Hanau
Mike

Hello Mike (@mgamer)

I believe you were very close to the solution. Hope the solution here attached is of help to your question.

20200504 Pikairos Select first n columns of a table.knwf (62.8 KB)

All the best

Ael

1 Like

You can use a TransposeNode, then “RowFilter by number” and then another TransposeNode.
If you want you can parametrize row number by flow variable in Row Filter Node.
KNIME_form_23302_pigreco.knwf (10.9 KB)

Hi @mgamer, Mike

The answer to this question can be found in your post Select columns of table at random . The Sampling node makes it possible to select the first n (say 50) columns.
Screenshot from 2020-05-04 19-40-25 .
gr, Hans

1 Like

Hi @mgamer,

I suggest using the Extract Column Header node and no prefixes in the configurations then by using the Column Filter node and this regex pattern, you can filter the first 50 columns:
[0-4]?[0-9]

After filtering, you can use the Insert Column Header node to have your original column headers back.

Here is an example workflow:

column_filter

23302-1-1.knwf (35.0 KB)

:blush:

1 Like

I’m afraid that regular expression is not applicable always because if you want to select the first 45 rows regular expression is not so simple.
:wink:

It is easy. To have only the first 45 columns:

[1-3]?[0-9]|[4][0-4]

If your column headers are already numbered, you don’t need to extract and insert column headers.

:blush:

To make things a bit easier for you, here is a simple method to create the regex pattern automatically. You just need to enter the number of columns in the Integer Input node (1 to 99 supported). You can convert this workflow to Component as well.

column_filter

23302-1-2.knwf (45.6 KB)

:blush:

We have lots of answers here, which is great! But unless am I crazy or missing some nuance - both possible - the OP is looking for the new Top k Selector node. :slight_smile:

2 Likes

@armingrudd ok. But I think that the keyword of this topic is the word “range”.
You’re building a regular expression, a powerful mechanism to generate a list of sequence that are satisfying your pattern.
The fact that all word that match your pattern are sequencially numbers is a detail because you are not defining a range, but only a list. A complete list of values that satisfy the goal.
This approach, according to me, is more complex that to define a single number as boundary, independently by column names.

What do you think about my solution in my first reply?

@pigreco transposing large tables can take quite a while and you are doing it 2 times in your workflow, so I would not suggest it. In my suggestion we don’t transpose the table at all. The Transpose node in my workflow is used for column headers not the table itself.

@ScottF topic is “select first n columns”. I think the reason that our friends here are suggesting different row filtering options is that they are searching for a solution regarding @mgamer’s idea (transposing and using reference column filter).

:blush:

1 Like

@armingrudd thanks a lot.
I don’t understand why transpose implementation is so heavy but now I want to find another solution. :slight_smile:
Thanks for the answer!

1 Like

If you want to use the Transpose node and the Reference Column Filter node, you can filter the first row of the table > transpose > row filter (filter a range of rows by number) > transpose > and finally use the reference column filter node. But why not filtering columns directly?

23302-1-3.knwf (35.6 KB)

:blush:

Indeed, I was missing something. I blame lack of caffeine. :slight_smile:

2 Likes

Hi,

Here is my solution

Filter columns from n to m.knwf (18.2 KB)

Hello everbody,

glad to see this got you fired up but is there anyone with a feeling that writing regex, transposing table (twice!) and other interesting approaches to this fairly simple request is a bit too much? :smiley:

Br,
Ivan

3 Likes

Hi @ipazin,

Well, I’m curious if there is a more straightforward solution to this problem other than using a column filter and a simple regex (or even better: a dynamic regex!).

Looking forward to learning something new. :nerd_face:

:blush:

Thinking about Java development? :smiley:

Br,
Ivan

1 Like

Of course using some code could be the solution, but I always think about pure KNIMEing… (if regex is counted!) :wink:

:blush:

3 Likes

A few days not KNIMEing and then such a lot of answers! Thank you very much. All of them are (different) solutions. Cool, but I often wonder why such a simple task (a one-liner in most other languages, especially Mathematica), need such solutions. KNIME is mostly table based - such basic operations should be implemented directly, just my opinion.