Skip to content

Instantly share code, notes, and snippets.

@npjc
Last active August 29, 2017 17:12
Show Gist options
  • Save npjc/e707b690f8f112e3ccc774e25a0c3e00 to your computer and use it in GitHub Desktop.
Save npjc/e707b690f8f112e3ccc774e25a0c3e00 to your computer and use it in GitHub Desktop.
how to use relative dose to compare across drugs
library(tidyverse)
#> Loading tidyverse: ggplot2
#> Loading tidyverse: tibble
#> Loading tidyverse: tidyr
#> Loading tidyverse: readr
#> Loading tidyverse: purrr
#> Loading tidyverse: dplyr
#> Conflicts with tidy packages ----------------------------------------------
#> filter(): dplyr, stats
#> lag():    dplyr, stats

make example data

d <- data_frame(
    drug = rep(letters[1:2], each = 4),
    dose = c(0.25, 0.5, 0.5, 1, .1, .2, .1, .2),
    expt = rep(c(1,1,2,2), 2)
)

so here we simluate testing 2 drugs (=drug) and b at 2 doses (=dose) in experiment (=expt) 1 then changes the two doses in the second experiment for drug a.

d
#> # A tibble: 8 x 3
#>    drug  dose  expt
#>   <chr> <dbl> <dbl>
#> 1     a  0.25     1
#> 2     a  0.50     1
#> 3     a  0.50     2
#> 4     a  1.00     2
#> 5     b  0.10     1
#> 6     b  0.20     1
#> 7     b  0.10     2
#> 8     b  0.20     2

if we want to make a new variable that is a relative dose so that the lowest drug dose for each drug is '1' and the next highest is '2' and so on we use the dense_rank() function as follows:

d %>% 
    group_by(drug) %>% 
    mutate(std_dose = dense_rank(dose))
#> # A tibble: 8 x 4
#> # Groups:   drug [2]
#>    drug  dose  expt std_dose
#>   <chr> <dbl> <dbl>    <int>
#> 1     a  0.25     1        1
#> 2     a  0.50     1        2
#> 3     a  0.50     2        2
#> 4     a  1.00     2        3
#> 5     b  0.10     1        1
#> 6     b  0.20     1        2
#> 7     b  0.10     2        1
#> 8     b  0.20     2        2
library(tidyverse)
#' make example data
d <- data_frame(
drug = rep(letters[1:2], each = 4),
dose = c(0.25, 0.5, 0.5, 1, .1, .2, .1, .2),
expt = rep(c(1,1,2,2), 2)
)
#' so here we simluate testing 2 drugs (=drug) and b at 2 doses (=dose) in
#' experiment (=expt) 1 then changes the two doses in the second experiment for
#' drug a.
d
#' if we want to make a new variable that is a standardized dose so that the
#' lowest drug dose for each drug is '1' and the next highest is '2' and so on
#' we use the dense_rank() function as follows:
d %>%
group_by(drug) %>%
mutate(std_dose = dense_rank(dose))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment