mardi 30 juillet 2019

Why is my code getting rid of rows when I subtract?

So I have some code that looks at two data frames and subtracts a column value named "Intensity" for certain Compositions of molecules. However for instance if the molecule is not in the other data frame, it completely gets rid of that row for some reason not too sure why.

blankdata3 and data3 are my two dataframes that I am subtracting. So I am subtracting a molecules's Intensity such as

(data3 - blankdata3) = datasubtracted


#This is my code that subtracts the molecule's Intensity based on if they have the same Composition. 

datasubtracted <- blankdata3 %>% left_join(select(data3, Intensity, Composition), by ="Composition") %>%
mutate(Intensity = ifelse (is.na(Intensity.y), -Intensity.x, Intensity.y - Intensity.x)) %>%
select(-Intensity.y, -Intensity.x ) %>%
bind_rows(anti_join(data3, blankdata3, by = "Composition") %>%
          mutate( Intensity = -Intensity))

#Example of data3 is below
#Composition Intensity
#C2H6O       100000
#C2H4O2      43000

#Example of blankdata3 is below
#Composition Intensity
#C2H6O       60000
#C3H8        53000

#When I run the code it gives me something like this (datasubtracted)
#Composition Intensity
#C2H6O       40000

#I expect to see this. I want compounds from data3 to carry over to datasubtracted untouched since there is nothing to subtract with. Basically subtracting a molecule by zero.
#Composition Intensity
#C2H6O       40000
#C2H4O2      43000


The code completely gets rid of molecules from data3 if they are not found in blankdata3 when the code runs, which I do not want.

Aucun commentaire:

Enregistrer un commentaire