0

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.

Marty999
  • 143
  • 1
  • 10
  • 1
    It works for me. Try to unload `plyr` if you attach it. As additional step you can update `dplyr` also – A. Suliman Aug 09 '18 at 19:31
  • Wow, looks like your right. Been pounding away at this for hours and the code was right the whole time. I had to re-install R and re-install all of my packages and still wasn't working. I can only get it to work when I load librady(dplyr) alone on a fresh boot. As soon as my other packages are loaded, I get this problem once more. Thanks for the input, I'll take down the question. Cheers! – Marty999 Aug 09 '18 at 19:58
  • I'm glad it helps, Please do so as the problem isn't reproducible anymore. – A. Suliman Aug 09 '18 at 20:07
  • Seems like I can only make this script function when `dplyr` is loaded alone. But I also need `reshape` and `plyr` to run my full script. When I load those, I get the error that `mutate` is masked from `package:plyr`. I'm wondering if you have some insights into this? Also, I cannot seem to find a way to delete the question, perhaps my rep is too low. – Marty999 Aug 09 '18 at 20:34
  • There is a conflict "name-collision" between `dplyr` and `plyr` in `group_by` and others verbs when they loaded together. Try calling functions explicitly e.g. `dplyr::group_by` or load and offload libraries when required. Also see [here](https://stackoverflow.com/questions/26923862/why-are-my-dplyr-group-by-summarize-not-working-properly-name-collision-with) – A. Suliman Aug 09 '18 at 20:46
  • Thank you once again for the help, hopefully I can resolve this. – Marty999 Aug 09 '18 at 21:00

0 Answers0