Create Simulation dataset

set.seed(2019)

LDLp <- abs(round(rnorm(711, 1299.7, 392.1), 2))

LDLc <- abs(round(rnorm(711, 126.6, 30.1), 2))

sim <- data.frame(LDLp, LDLc)

Calculate LDL-C and LDL-P percentile ranks

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
simrank <- sim %>%
  mutate(LDL_C_percentile = ecdf(LDLc)(LDLc) * 100,
         LDL_P_percentile = ecdf(LDLp)(LDLp) * 100) # This code uses the ecdf function to calculate the percentile ranks of LDL_C and LDL_P

# /* Create variable for concordance/discordance */
  simgroups <- simrank %>%
  mutate(difference = LDL_C_percentile - LDL_P_percentile) %>%
  mutate(LDL_concordance = case_when(
    difference >= -12 & difference <= 12 ~ "Concordant",
    difference > 12 ~ "LDLc>LDLp",
    difference < -12 ~ "LDLp>LDLc"
  ))

  table(simgroups$LDL_concordance)
## 
## Concordant  LDLc>LDLp  LDLp>LDLc 
##        146        276        289

Different way

  # Calculate LDL-C and LDL-P percentile ranks
  library(dplyr)
  sim_ranked <- sim %>%
    mutate(LDL_C_percentile = percent_rank(LDLc),
           LDL_P_percentile = percent_rank(LDLp))
  
  # Create groups
  sim_groups <- sim_ranked %>%
    mutate(ldl_diff = LDL_P_percentile - LDL_C_percentile,
           group = ifelse(ldl_diff <= -0.12, "LDL-P < LDL-C",
                          ifelse(ldl_diff >= 0.12, "LDL-P > LDL-C", "Concordant")))

  table(sim_groups$group)
## 
##    Concordant LDL-P < LDL-C LDL-P > LDL-C 
##           146           276           289

Calculate percentile

 # Calculate the 25th percentile
  p <- quantile(sim$LDLp, probs = 0.250)
  p
##    25% 
## 1004.4
  c <- quantile(sim$LDLc, probs = 0.250)
  c
##    25% 
## 106.97
# Calculate the 10th, 30th, and 70th percentiles
percentiles <- c(20, 25, 50)
quantile(sim$LDLp, probs = percentiles/100)
##     20%     25%     50% 
##  961.58 1004.40 1246.08
quantile(sim$LDLc, probs = percentiles/100)
##    20%    25%    50% 
## 103.34 106.97 128.40
# Discordance of LDL Cholesterol with Alternative LDL-Related Measures and Future Coronary Events
# Discordance between apolipoprotein B and low-density lipoprotein particle number is associated with insulin resistance in clinical practice