right now I'm refactoring an 'base'-based R script by using 'dplyr' instead.
Basically, I want to group_by Gene and subtract the values group-wise by a group that matches a given condition. In this case, I want values of Gene == 'C' and subtract them from all others.
Simplified data:
x <- data.frame('gene' = c('A','A','A','B','B','B','C','C','C'),
'sample' = rep_len(c('wt','mut1','mut2'),3),
'value' = c(32.3,31,30.5,25,25.3,22.1,20.5,21.2,19.8))
gene sample value
1 A wt 32.3
2 A mut1 31.0
3 A mut2 30.5
4 B wt 25.0
5 B mut1 25.3
6 B mut2 22.1
7 C wt 20.5
8 C mut1 21.2
9 C mut2 19.8
Desired output:
gene sample value deltaC
1 A wt 32.3 11.8
2 A mut1 31.0 9.8
3 A mut2 30.5 10.7
4 B wt 25.0 4.5
5 B mut1 25.3 4.1
6 B mut2 22.1 2.3
7 C wt 20.5 0.0
8 C mut1 21.2 0.0
9 C mut2 19.8 0.0
I base, it's not a big deal, but I'm wondering whether there is a simple solution using dplyr.
'Pseudo'code:
df %>%
group_by(Gene) %>%
mutate(deltaC = Value - Value(where Gene == 'C'))
Is there any kind of function that allows me to access only those values of Gene == 'C'? Of course I could also do a subset before, but I would like to do it in one step :)