R: Run a function until conditional test on the previous output of function is True for all rows
I'm trying to write a script in R that repeats a conditional test or function until all values meet that condition. I can accomplish this by copying and pasting, but as we know this will get cumbersome. Here is a sample of what I'm trying to do, in this case, get all rows to NA.
#Create the data:
df <- data.frame(x = rnorm(20))
#Round 1 conditional test: if the number from rnorm is < 0 add 0.25, else NA
df_1 <- df %>% mutate(test_1 = ifelse(df$x < 0, df$x + 0.25, NA))
#Round 2 conditional test: if the number from df_1$test_1 is < 0 add 0.25, else NA
df_2 <- df_1 %>% mutate(test_2 = ifelse(df_1$test_1 < 0, df_1$test_1 + 0.25, NA))
#Round n conditional test: repeat the ifelse until all values are > 0 & thus all rows are NA
I'd like to know if there is a way to do this where we could apply the function as many times as needed until our condition is met?
Thanks!
from Recent Questions - Stack Overflow https://ift.tt/3d8pE18
https://ift.tt/eA8V8J
Comments
Post a Comment