I am trying to remove the background noise from a signal in a dataset. To do this, I must subtract the signal at say 6 hours minus the signal at 0 hours. This signal changes depending on the Sample, Category, and Metal treatment.
I realize this question has been asked in the past here, including myself once before here. I went through my old code and what used to work 1 year ago is no longer working. I am wondering if there has been a change to the dplyr
package that affected this function?
I group everything using the group_by
function and then apply the math using the mutate
function.
Current code
library(dplyr)
MyData <- MyData %>%
group_by(Sample, Category, Metal) %>%
mutate(SignalNoise = Signal - Signal[Time.h==0])
Data Table (with output)
Time.h Sample Category Metal Signal SignalNoise
0 1 A A 2 0
2 1 A A 22 21
4 1 A A 42 38
6 1 A A 57 55
0 2 A A 1 0
2 2 A A 11 7
4 2 A A 51 49
6 2 A A 101 100
0 3 B A 4 0
2 3 B A 204 202
CSV file here.
Notice how the first row of each group is correct: the background signal (T=0) is subtracted from the signal. But every subsequent time point within the group is subtracted by the value of another group... For instance, second third and fourth rows should have been subtracted by 2 (like the first row), but rather they are subtracted by 1, 4, and 2. These are the T=0 noise values of other groups.
The code that used to work for me in the past is the following:
... group_by(...) %>% mutate(SignalNoise = Signal - Signal[1])
This no longer works. It ignores all the groups and subtract the very first value to all other values. I have a hard time making sense of this, your help here would be greatly appreciated.