First of all, I couldn't find a question related with my issue, apologies if this question was already answered.
I have a dataframe with some columns and I want to calculate a new value using a specific ecuation. I guess I have to use mutate() from tidyverse, but I want to avoid rows/samples where there're one or more 0 values. I don't know how I can check if there's any 0 when I'm using mutate(). Also, I don't know how I can apply my specific formula to create the new column.
I leave here a code to create a dataframe as an example of my issue.
set.seed(123)
df <- data.frame(
time = seq(now(), now()+hours(11),by='hours'),
a = sample(0:100,12),
b = sample(0:100,12),
c = sample((0:20)/1000,12))
df[1:3,]$a <- 0
df[3:5,]$b <- 0
df[3:4,]$c <- 0
# function: M = a*b+(1-e^(-c/2))
# if any 0 in the row -> M = NA
# else: apply function
The function could be written as
a*b*(1-exp(-c/2))
The final df should have 4 colums per each hour (row) (a,b,c and the new calculated M), but when a | b | c == 0, M = NA.
I will be very grateful for every little help. Cheers!
EDIT: The real function is more complex that this example, so it will not be always true that if one term (a,b,c,...) is 0, the resulting M is 0. Sorry, I didn't realised this postulate is true for the simplified equation. But I want to avoid any 0 value because they are from monitoring physiological variables and I know if one value is 0 in a sample, then the sample is wrong, so NA.