-1

I have read a lot of "how to get a percentile" answers but could not find a solution to my problem.

I have a data.frame with 3 columns: Heart rate (beats per minute), Temperature (ranging from 35 to 45 - without decimals), Activity (ranging from 1 to 15 - without decimals).

I would like to add a 4th column with the percentile value of the Heart rate considering the distribution of Heart rate characterized by a given Temperature and a given Activity.

Example: at Temperature=37 and Activity=5 a Heart rate of 60 beats per minutes has a percentile of ...

#example of data frame
n = 1000
df <- data.frame(HeartRate = round(runif(n, 60, 100)),
                 Temperature = round(runif(n, 35, 45)),
                 Activity = round(runif(n, 1, 15)))

Thank you very much in advance for your help.

Luc
  • 33
  • 4
  • Please add an example of your dataset and see here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – abichat Aug 25 '17 at 09:38

1 Answers1

0

Try this? If it doesn't meet your purpose, please provide a sample of your data & what you expect the output to look like.

library(dplyr)

# generate dummy data
n = 1000
df <- data.frame(HeartRate = round(runif(n, 60, 100)),
                 Temperature = round(runif(n, 35, 45)),
                 Activity = round(runif(n, 1, 15)))

df %>% group_by(Temperature, Activity) %>%
  mutate(Percentile = percent_rank(HeartRate)) %>%
  ungroup()
Z.Lin
  • 28,055
  • 6
  • 54
  • 94