Message Boards Message Boards

GROUPS:

[✓] Get a chart of frequency data?

Posted 10 months ago
737 Views
|
2 Replies
|
2 Total Likes
|

Hello, I can't figure out how I can get a Chart from data that are in the following form: {{-5,4},{-2,7},{-1,3},{0,1},..} where I "measured" the value -5, 4 times, the value -2, 7 times and so on, meaning the table shows the frequency of the value appearing. I know if the data would have the form {-5,-5,-5,-5,-2,-2,...} I could use Histogram, but I don't know how to do it when the frequency is already counted.

I also would like to calculate the expected value and the standard deviation.

Thanks a lot.

2 Replies

What about this?

dat = {{-5, 4}, {-2, 7}, {-1, 3}, {0, 1}};
dat1 = #[[1]]*#[[2]] & /@ dat
ListPlot[dat1]

or

Table[#[[1]], {#[[2]]}] & /@ dat
Flatten[%]
Histogram[%]

Additionally these might work. Suppose you have data:

dataorig = RandomInteger[{-10, 10}, 1000];

You can then generate a list of values vs repetitions like so:

data = SortBy[Tally[dataorig], First];

Of course, you can always reconstruct something with the same distribution as the original:

datarestore = Flatten[ConstantArray[#[[1]], #[[2]]] & /@ data];

The way described in the first answer is more elegant:

datarestore = Table[#[[1]], {#[[2]]}] & /@ data

Note that you can get a histogram directly from the data by using a BarChart:

BarChart[data[[All, 2]], ChartLabels -> data[[All, 1]]]

enter image description here

From the restored data it is easy to compute the mean and the standard deviation:

N@Mean[datarestore]

and

N@StandardDeviation[datarestore]

You can also use WeightedData to achieve something that you can easily evaluate:

dataweight=WeightedData @@ Transpose[data]

and then

N@Mean[dataweight]

and

N@StandardDeviation[dataweight]

Interestingly:

N@StandardDeviation[dataweight] == N@StandardDeviation[datarestore]

is False.

Cheers,

Marco

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