Truncating or setting limits to a variable

Hello! I'm kinda new to Rstudio. Could someone show me how to put limits in a variable?

I have a microbial growth equation:

Bacteria (log/egg)= (-0.143+0.026*initialconcentration^2)*timeinhours

But when I calculate it significantly exceeds the highest possible number of logs of bacteria per egg (it should only be up to 8 logs), how do I truncate or set a limit to this in R? I want to set the limits of the output to 1 log as minimum and 8 logs as maximum.

Thank you so much!

# in base r
(nums <- runif(10,min=0,max=100))
(nums2 <- ifelse(nums>50,50,nums))
(num_fixed <- ifelse(nums<20,20,nums2))

# with dplyr package
(alternate_num_fixed <- dplyr::case_when( nums > 50 ~50,
                                         nums < 10 ~10,
                                         TRUE ~ nums))
1 Like

Thank you for replying! I followed your ifelse method!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.