1
$\begingroup$

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.

$\endgroup$
5
  • 3
    $\begingroup$ 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}] $\endgroup$
    – Artes
    Sep 19, 2013 at 9:03
  • 2
    $\begingroup$ maybe add Floor, Ceiling, IntegerPart or some Round there - unless you can ensure that Length[data]/10 is integer-valued $\endgroup$ Sep 19, 2013 at 9:17
  • $\begingroup$ @PinguinDirk Simply assumed his data to be Range@100. $\endgroup$
    – Artes
    Sep 19, 2013 at 9:21
  • $\begingroup$ 10% would be length of the data. $\endgroup$
    – Lithium27
    Sep 19, 2013 at 9:48
  • 1
    $\begingroup$ Top and bottom X% by length or by value? Consider TrimmedMean if that is what you are intending to calculate. $\endgroup$
    – Verbeia
    Sep 19, 2013 at 11:31

2 Answers 2

1
$\begingroup$

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]]
$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – Verbeia
    Sep 19, 2013 at 11:30
  • $\begingroup$ add an if statement: If that = 0 then use 1 else use the answer it gives you... It was just a rough way answer. $\endgroup$
    – lara
    Sep 19, 2013 at 12:35
1
$\begingroup$

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}
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.