Message Boards Message Boards

Rearranging data in a list

Posted 10 years ago

Im wondering what is the quickest way to rearrange a list of data points with error as to fit it into the standard format that allows the use of "ErrorListPlot".

When i store data in a csv file, Ill list the x, y, and y error in 3 separate columns. Importing that data to mathematica creates a list that looks like {{x1,y1, error y1}, {x2,y2,error y2}, ...}.

The format that is required for ErrorListPlot is {{{x1,y1}, ErrorBar[error y1]}, {{x2,y2}, ErrorBar[errory y2]}} ....}. What is the best way to reformat the list to get this form?

POSTED BY: Man Nguyen
2 Replies

Let's say your list is called data, i.e.,

data = {{x1,y1, error y1}, {x2,y2,error y2}, ...}

then this will put it in the form you want

data /. {x_?NumberQ, y_?NumberQ, ey_?NumberQ} :> {{x, y}, ErrorBar[ey]}

Here's a test:

data = RandomReal[1, {5, 3}];

data /. {x_?NumberQ, y_?NumberQ, ey_?NumberQ} :> {{x, y},  ErrorBar[ey]} 
POSTED BY: David Reiss

A rule would probably be the simplest way to do it.

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract