2

I have a dataset which looks like this:

0 1 0.1
0 0 0.1
0 1 0.1
1 0 0.2
0 1 0.2
1 0 0.2
...

I now want to do the following operations on each different value in the third column of the table:

Example for 0.1:

First column values summed: 0+0+0=0

Second column values summed: 1+0+1=2

Now I want to substract these two 2-0=2 and in a last step divide them by the occurrences.

2/3 =0.667

The same for 0.2 and my plot should then plot at x=0.1, y=0.667.

I hope my problem is with the example understandable.

0

1 Answer 1

4

You can use the smooth unique option to do exactly this: sum up all y-values belonging to the same x-value and then divide the result by the number of occurences. For the second column, upon which the operation is performed, you use the difference between the second and first column:

plot 'file.txt' using 3:($2 - $1) smooth unique

However, it seems like you'll run in a strange bug then. This works only correct, if you insert an empty or commented row at the beginning of your data file:

The result with the following file.txt

#
0 1 0.1
0 0 0.1
0 1 0.1
1 0 0.2
0 1 0.2
1 0 0.2

is

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

As an annoying side note, this is working only because you are just subtracting values, i.e. you are not really summing all items in column(2) then subtracting the sum of all items in column(1), in reality you are summing all the (item2 - item1). Since this is a linear operation, sum(item2) - sum(item1) is the same as sum(item2 - item1), but if you wanted to do something else, e.g. sum(item2)/sum(item1), then you'd need a more complicated approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.