Closed ThorstenGravert closed 1 year ago
I am not familiar with dplyr
but it seems that case_when
(similar to ifelse
) first executes all statements on the right and than subset the results based on the logical expression on the left. That means subtractElements
is run for the whole data.frame
. Nevertheless it would be good to handle NA
in subtractElements
.
agree @sgibb that handling NA
would be good in subtractElements
- could you maybe add an issue for that?
Hi all, I'm currently exploring the MetaboCoreUtils and it's super useful, well done. For some lipidomics processing, I tried to use subtractElements to calculate the tail composition of annotated lipids. Simple enough, the idea is to take the sum formula and subtract the headgroup composition (if known).
However, subtractElements seems to have issues with NA values. I use a dplyr pipe to calculate the tail group for all rows (lipid entries) that have a head group composition. My natural approach would be to use a case_when statement, with the condition to apply subtractElements only to items, not NA (!is.na). But I get an error, related to the countElements function, used in subtractElements. Interestingly, if I filter my data frame first, to remove all rows containing NAs in the headgroup composition field, the functions work fine and I get the desired results.
I made an example below with the code for my pipe, one working, the other failing and I pasted the error output.
Maybe one of you knows what causes the issue and if there is a better solution than filtering and then joining.
The following works fine, but I will need to join the output back together with the initial dataframe.
The following should work fine (since I'm excluding NA values in the case_when statement).
But it returns following error: