I have a table with many samples. I would like to have Positive & Negative controls at the top of the table, but need help figuring out the most straighforward way to do this.
Here is a minimal example dataframe:
df = data.frame(structure(list(Well = c("A1", "A2", "B1", "B2"),
Sample = c("Asample 2", "Positive control", "Asample 1", "Negative control"))))
The task:
- Positive & negative controls at the top of the table.
- The rest of the table arranged by
well
and NOT thesample
name. - Keep in mind that positive & negative controls are always named like this, but could have any random well id in real life situation, whereas sample names and well ids can vary.
The desired output:
well sample
<chr> <chr>
1 A2 Positive control
2 B2 Negative control
3 A1 Asample 2
4 B1 Asample 1
CodePudding user response:
Another solution:
df %>% arrange(match(Sample,c("Positive control", "Negative control")),Well)
Well Sample
1 A2 Positive control
2 B2 Negative control
3 A1 Asample 1
4 B1 Asample 2
CodePudding user response:
One solution would be to make sample
a factor()
using well
levels, and then to relevel sample
appropriately.
df %>%
arrange(Well) %>%
mutate(
Sample = fct_relevel(
fct_inorder(factor(Sample)),
c('Positive control', 'Negative control')
)
) %>%
arrange(Sample)
Well Sample
1 A2 Positive control
2 B2 Negative control
3 A1 Asample 2
4 B1 Asample 1