lundi 3 août 2020

Code in R to conditionally subtract columns in data frames

I have a data frame from a measurement where for each measurement a background is also measured:

 Wavelength   Background_1   1   Background_2   2   ...
 300          5              11  4              12  ...
 301          3              12  5              10  ...
 ...          ...            ... ...            ... ...

I want to subtract the appropriate "Background_xyz" column from the corresponding column (e.g. subtract "Background_1" from "1". It would then look like this:

 Wavelength   1_corrected   2_corrected   ...
 300          6             8             ...
 301          9             5             ...
 ...          ...           ...           ...

I can get this far no problem. The problem is, sometimes there are 3 measurements, so 3 columns with background and "real" data each, sometimes there are only 1 or 2 measurements. I am looking for a way to have R "correct" columns by subtracting the background independent of the number of columns to do so. I was thinking maybe an if function checking for the column names would to the trick but I am not experienced enough to figure out a way to do that yet. Help is greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire