Add percentile each value is and put it into new column

Suppose I have a simple column of data:

mydata <- data.frame("hp"=mtcars$hp)
head(mydata)

hp
1 110
2 110
3  93
4 110
5 175
6 105

How can I add a second column that calculates the 'percentile' value? For example, the first value 110 is the nth% of the hp column - and repeat for all rows?

I'm looking into quantile but trying to apply this to all rows

I think you want the ecdf function rather than quantile.

mydata <- data.frame("hp"=mtcars$hp)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
ECDF <- ecdf(mydata$hp)
mydata <- mydata %>% mutate(Perc = ECDF(hp))         
head(mydata)
#>    hp    Perc
#> 1 110 0.43750
#> 2 110 0.43750
#> 3  93 0.21875
#> 4 110 0.43750
#> 5 175 0.68750
#> 6 105 0.31250

Created on 2021-05-04 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.