'how to apply() a function only to rowSums() > 0

any thoughts on how to apply a function to only row where there is at least 1 value more than 0?

I tried:

    batch4 =
  raw_mat[,all_conds == "LR140 AM"] %>%
  apply(., 1, FUN = function(x) ifelse(sum(x) > 0,  zCompositions::cmultRepl(., output = "p-counts", z.warning = 1), x))
Error in rowSums(x) : 'x' must be an array of at least two dimensions 

but it seems rowSums is only taking 1 element of the row at the time?

    > batch4 =
+   raw_mat[,all_conds == "LR140 AM"] %>%
+   rowwise() %>% 
+   mutate(across(where(rowSums(.) > 0), zCompositions::cmultRepl(., output = "p-counts", z.warning = 1)))
Error in `mutate()`:
! Problem while
  computing `..1 =
  across(...)`.
ℹ The error occurred in row
  0.
Caused by error in `zCompositions::cmultRepl()`:
! Row(s) containing all zeros/unobserved values were found (check it out using zPatterns).
Run `rlang::last_error()` to see where the error occurred.

So in this case it looks like the rowSums filter is not working properly, as cmultRepl gets also "only zeros" rows.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source