I have been searching for quite some time to an elegant solution to this problem, to no avail. So I decided to give it a go here.
I am using tidyverse
, and
The *_join
functions from dplyr
(loaded with tidyverse
) are great for solving lots of problems involving more than one dataframe.
> df %>%
gather(sample, value, -annot) %>%
left_join(sample_info, by = 'sample')
annot sample value condition
1 A sample1 1 infected
2 B sample1 1 infected
3 C sample1 4 infected
4 D sample1 2 infected
5 A sample2 3 uninfected
6 B sample2 5 uninfected
7 C sample2 4 uninfected
8 D sample2 5 uninfected