I have a data file which contains thousands of lines and each line has eight elements. Here is a small sample of the data file
-4.00 -0.80 0.1886024468848907E+01 0.1467147621657460E+01 -.1217067274319363E+01 0.7206100000000000E+03 0.7693457688734395E-12 5
-4.00 -0.70 0.1430357986632780E+01 -.1404093461650013E+01 -.1742223680347601E+01 0.1824700000000000E+03 0.8439003681169850E-12 8
-4.00 -0.60 -.1324719768465547E+01 0.1740130076002850E+01 0.1497978622206873E+01 0.5479900000000000E+03 0.4264485578634903E-14 2
-4.00 -0.50 0.1358536876189560E+01 -.1580696533502541E+01 0.1621539980382560E+01 0.2881100000000000E+03 0.1319885603098060E-13 4
-4.00 -0.40 -.1588487538231399E+01 0.1275577589608218E+01 0.1707607247015512E+01 0.1337500000000000E+03 0.1057878487713421E-12 2
-4.00 -0.30 0.1755414125374284E+01 0.1332710827520201E+01 0.1477984475201826E+01 0.6426400000000000E+03 0.1459764022611444E-13 1
-4.00 -0.20 0.1245972697710741E+01 0.1540633564543885E+01 0.1777167629372046E+01 0.6112200000000000E+03 0.5718386586661092E-13 1
-4.00 -0.10 -.1311461418732105E+01 -.1594149065989313E+01 0.1661344176980193E+01 0.3507800000000000E+03 0.6765588799377521E-14 3
I read this file using
data = ReadList["data.out", Number, RecordLists -> True];
The total length of the list is obtained, of course as
ntot = Length[data];
The list contains eight elements per row and the last of them is an integer taking values in the interval [0,8]. What I want is the following:
(a). Count how many rows have 0 value at the last element (let's suppose there are n0
), how many have 1, 2, 3, ... , 8. Then calculate the corresponding percentages per0 = n0/ntot
, per1 = n1/ntot
, etc. It could be nice if this was inside a DO loop with i = 0,8
.
(b). Count again percentages but using more than one criteria this time. For example, count how many rows have 1 at the last element and the value of the seventh element is smaller than 10^{-4}
.
Any suggestions?
EDIT
Using's @Kuba's solution we have
{{5, 656}, {8, 640}, {2, 673}, {4, 663}, {1, 673}, {3, 663}, {6, 656}, {7, 640}, {0, 19}}
Is it possible to divide each sum automatically with ntot
thus obtaining the percentages?
{{5, 656/ntot}, {8, 640/ntot}, {2, 673/ntot}, {4, 663/ntot}, {1, 673/ntot}, {3, 663/ntot}, {6, 656/ntot}, {7,640/ntot}, {0, 19/ntot}}
Also it would be great if they were sorted from 0 to 8 not randomly as they are now.
data[[ ;; , 8]] // Tally
there is all what you need to calculate those %. $\endgroup$Count
orTally
, now you are asking forSortBy
and at the endCases
orSelect
. And this is a problem because it shows no research effort :/ $\endgroup$Divide
isListable
so you can justSortBy[data[[;; , 8]] // Tally, 1][[;;, 2]] / ntot
. For extended criteria useCases
orSelect
. $\endgroup$