Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a custom ifelse based in proportion limit values

I’d like to create some trick using a custom ifelse based in a rule comparing dates. If the value in the next date is 10% higher
comparing with later date then repeat the old value, if not accept the new value. Using artificial values:

# Packages
library(dplyr)
library(lubridate)

# Dataset
area <- c("A","A","A","B","B","B","C","C","C")
dates <- c("05-01-2024","10-01-2024","18-01-2024",
"05-01-2024","10-01-2024","18-01-2024","05-01-2024",
"10-01-2024","18-01-2024")
response <- c(63,68,82,77,71,60,79,75,73)
my.ds <- data.frame(area=area,dates=dates,response=response)

# 
max.prop <- 0.1
my.ds$dates <- lubridate::dmy(my.ds$dates)

# Here I don't have success in using ifelse invoke values at a later date. 
# My output must be:

# my.ds.new
#  area      dates response
#1    A 2024-01-05       63
#2    A 2024-01-10       68
#3    A 2024-01-18       68
#4    B 2024-01-05       77
#5    B 2024-01-10       71
#6    B 2024-01-18       71
#7    C 2024-01-05       79
#8    C 2024-01-10       75
#9    C 2024-01-18       73

Plaese, any help wit it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

my.ds.new <- my.ds %>% mutate(response = ifelse(c(0, abs(diff(response))) > abs(response * .1) & 
                                                  lag(area) == area, 
                                                lag(response), 
                                                response))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading