[R] quantiles and dataframe
Anders Bjørgesæter
anders.bjorgesater at bio.uio.no
Fri Sep 14 13:23:10 CEST 2007
Hi
I have a dataframe, RQ, like this:
A B1 B2 B3
1 NA 112 12
2 NA 123 123
3 NA 324 13
4 3 21 535
5 4 12 33
6 7 1 335
7 4 NA 3535
8 4 NA NA
9 NA NA NA
10 5 NA NA
12 4 NA NA
15 2 NA NA
17 3 NA 1
63 1 NA 1
75 NA NA NA
100 NA NA NA
123 NA NA NA
155 NA NA NA
166 NA NA NA
177 NA NA NA
I want to extract min, max, 5% and 95% from A based on the range of the Bs.
Using this:
s1<-A[min(which(!is.na(B1))):max(which(!is.na(B1)))]
q1<-quantile(s1,probs=c(0,5,95,100,NA)/100)
I manage to get this by changing the B1 manually for each B
B1 B2 B3
4.0 1.00 1.00 (min)
63.0 6.00 63.00 (max)
4.5 4.5 1.65 (5%)
40.0 6.00 63.00 (95%)
I tried to use apply like this: s1<-apply(RQ,2,function(x)
{A[min(which(!is.na(RQ[,2:4]))):max(which(!is.na(RQ[,2:4])))] })
to get the range of each B but that doesn't work.
Also as you see, s1 includes the A where the B's are NA, e.g. for B1 I
get the 9 at row 9 (4,5,6,7,8,9,10,12,15,17,63) and not
(4,5,6,7,8,10,12,15,17,63), which I would prefer.
BUT the main question is how can I extract min, max etc. from each B in
dataframe RQ without using a loop?
Any help is greatly appreciated!
Best Regards
Anders
More information about the R-help
mailing list