# Taking a percentage of data from a list or a table

I have a list of data and for the calculation I only need the a of portion of the data. I know that Take[data, {1,100}], for example, is taking line 1 to 100 of the file. But what I need it for me is to take percentage of the data like, drop first 10% and drop last 10% of the data. So I'll only have 80% of the middle portion to work with. So that if I have multiple list with amount of data changing I would not have to keep counting the number of lines or length to calculate. Thanks.

-
Since you haven't explained what it means "10%" of data, the only measure is its length, so it might be the solution you are looking for Take[ data, { Length[data]/10, -Length[data]/10}] –  Artes Sep 19 '13 at 9:03
maybe add Floor, Ceiling, IntegerPart or some Round there - unless you can ensure that Length[data]/10 is integer-valued –  Pinguin Dirk Sep 19 '13 at 9:17
@PinguinDirk Simply assumed his data to be Range@100. –  Artes Sep 19 '13 at 9:21
10% would be length of the data. –  Lithium27 Sep 19 '13 at 9:48
Top and bottom X% by length or by value? Consider TrimmedMean if that is what you are intending to calculate. –  Verbeia Sep 19 '13 at 11:31

You can specify the rows that you want as below, for example from 20% to 80% :

Data[[Round[Length[Data]*0.2,1] ;; Round[Length[Data]*0.8, 1]]]


From this above you could create your own function that does this for you so if you want to do this often you don't have to use all the text as above. Function Definition:

TakeDataPercent[DataSetName_, StartPercentage_, EndPercentage_, LengthofData_] :=
DataSetName[[Round[LengthofData*StartPercentage, 1] ;; Round[LengthofData*EndPercentage, 1]]]


And then to use the function you would simply write:

TakeDataPercent[Data, 0.2, 0.8, Length[Data]]

-
What if the rounded value Round[LengthofData*StartPercentage, 1] is 0? I think you need a few extra checks in here. I also don't think you need the final parameter - just let the function get the Length of the data directly. Otherwise you could have a last parameter that is not compatible with the data. –  Verbeia Sep 19 '13 at 11:30
add an if statement: If that = 0 then use 1 else use the answer it gives you... It was just a rough way answer. –  Lara Jordan Sep 19 '13 at 12:35

A dirty hack:

Unprotect[Span];
Span[b_Real, e_Real] := RealSpan[b, e];
RealSpan /: list_[[RealSpan[b_Real, e_Real]]] :=
list[[1 + Round[b Length[list]] ;; Round[e Length[list]]]];
Protect[Span];

n = Range[100];
n[[0.1 ;; 0.9]]

{11, 12, 13, 14, 15, ..., 86, 87, 88, 89, 90}

-