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.
2 Answers
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]]
-
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 theLength
of the data directly. Otherwise you could have a last parameter that is not compatible with the data. $\endgroup$– VerbeiaSep 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$– laraSep 19, 2013 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}
"10%"
of data, the only measure is its length, so it might be the solution you are looking forTake[ data, { Length[data]/10, -Length[data]/10}]
$\endgroup$Floor
,Ceiling
,IntegerPart
or someRound
there - unless you can ensure thatLength[data]/10
is integer-valued $\endgroup$data
to beRange@100
. $\endgroup$TrimmedMean
if that is what you are intending to calculate. $\endgroup$