Search code examples
gnuplot

How to join table columns in gnuplot?


How can I create a column as a combination of two (or more) columns? I have to select the desired columns by name:

Table:

A   B   C   D
1   5   7   4
2   8   4   9
8   5   7   4

It should become:

AC  BD
1   5
2   8
8   5
7   4
4   9
7   4

Solution

  • Here is one possible way with gnuplot to add columns (or concatenate or join) with writing also a header. Use the potting style with table and write the new data into a datablock. Default column separator is whitespace (if you write it to a file you will see that it will be TAB with an additional space). But you can set any other character, check help table.

    Script:

    ### join columns
    reset session
    
    $Data  <<EOD
    A   B   C   D
    1   5   7   4
    2   8   4   9
    8   5   7   4
    EOD
    
    set table $Joined
        plot '+' u ("AC    BD") every ::::0 w table, \
           $Data u "A":"B" w table, \
           $Data u "C":"D" w table
    unset table
    
    print $Joined
    ### end of script
    

    Result:

    AC    BD
     1       5
     2       8
     8       5
     7       4
     4       9
     7       4