Anyway I managed to sort it out with the below code

paolo

################################################à

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)

myecdf2 <- function(x, column, sortAsc="True") {
# x data.frame/list being analysed
# column to calculate percentile 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)
names(w3)[ncol(w3)] <- paste("Perc.",column,sep="")
return(w3)
}

myExampleEnd2 <- lapply(split(myExample, myExample$Sectors), myecdf2,
column="PE", sortAsc="False")
myExampleEnd2 <- unsplit(myExampleEnd2, myExample$Sectors)



2011/3/10 Paolo Cavatore <pcavatore@gmail.com>

> tapply doesn't work with ecdf
>
> myExampleEnd4 <- tapply(myExample, myExample$Sectors, ecdf)
> #gives error
>
> 2011/3/10 Hesen Peng <hesen.peng@gmail.com>
>
>  tapply is the function
>>   On Mar 10, 2011 4:38 AM, "Paolo Cavatore" <pcavatore@gmail.com> wrote:
>> > Hello,
>> >
>> > I'm trying to get percentiles (PERCENTRANK for excel users) by factor in
>> the
>> > following data.frame:
>> >
>> > 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-SIG-Finance@r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> > -- Subscriber-posting only. If you want to post, subscribe first.
>> > -- Also note that this is not the r-help list where general R questions
>> should go.
>>
>
>

	[[alternative HTML version deleted]]

