mercredi 19 février 2020

R dplyr function putting mutate, top_frac and ifelse together

Im looking for ways to mutate a new column to assign the top and bottom 20% of values using dplyr.

Here is my code and it isnt working well for me.

DF1 <- DF %>%
  group_by(Timepoint) %>%
  filter (!is.na (log2_Concentration)) %>%
  arrange (desc(log2_Concentration)) %>%
  mutate (top_bottom=ifelse (log2_Concentration=top_frac(.2), "TOP20PERC",
          ifelse (log2_Concentration=top_frac(-.2), "BOTTOM20PERC", "MID")))

My hope is to assign per timepoint, the top 20%, bottom 20% and the rest as MID so I can either color these points in my ggplot.

Thanks a lot gurus!

Aucun commentaire:

Enregistrer un commentaire