# [R] getting percentiles by factor

From: Paolo Cavatore <pcavatore_at_gmail.com>
Date: Thu, 10 Mar 2011 10:37:17 +0100

Hello,

myExample <- data.frame(Ret=seq(-2, 2.5, by=0.5),PE=seq(10,19),Sectors=rep(c("Financial","Industrial"),5)) myExample <- na.omit(myExample)

Thanks to Patrick I I managed to put together the following lines which does it for the "Ret" column:

myecdf <- function(x, sortAsc) {

```w1 <- ecdf(x\$Ret)
w2 <- if (sortAsc) w1(x\$Ret) * 100 else abs(w1(x\$Ret) * 100 - 100)
w3 <- transform(x, myPerc=w2)
```

return(w3)
}
myExampleEnd <- lapply(split(myExample, myExample\$Sectors), myecdf, sortAsc="True")
myExampleEnd <- unsplit(myExampleEnd, myExample\$Sectors)

I need to make the function more flexible accepting the name of the column to calculate percentiles on as a parameter but the following doesn't work:

myecdf2 <- function(x, column, sortAsc=True) {
# x data.frame/list being analysed
# column to calculate percentiles on
# sortAsc sorting order (True Ascending, False Descending)

```w1 <- ecdf(x\$column)
w2 <- if (sortAsc) w1(x\$column) * 100 else abs(w1(x\$column) * 100 - 100)
w3 <- transform(x, myPerc=w2)
```

return(w3)
}
myExampleEnd2 <- lapply(split(myExample, myExample\$Sectors), myecdf2, column=Ret, sortAsc="True")
myExampleEnd2 <- unsplit(myExampleEnd, myExample\$Sectors)

I'm not sure whether I'm going down the right way so any help is appreciated...also from scratch.

Paolo

[[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 10 Mar 2011 - 11:46:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Mar 2011 - 13:40:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.