DanChaltiel / EDCimport

Import data from EDC softwares
https://danchaltiel.github.io/EDCimport/
GNU General Public License v3.0
0 stars 1 forks source link

`manual_correction()` is not really usable #7

Open DanChaltiel opened 1 year ago

DanChaltiel commented 1 year ago

Current use:

l = with(data, which(subjid==111 & dqlq=="2020/03"))
manual_correction(data, dqlq, rows=l, wrong="2020/03", correct="2021/03")

wrong = ymd("2011/11/29", "2021/01/12", "2019/12/11", "2020/12/28")
l=which(data$tc_dt %in% wrong & data$subjid  %in% c(128,196,17,73))
wrong = data$tc_dt[l]
manual_correction(data, tc_dt, rows=l, wrong=wrong, correct=rep(NA, 4))

Problem: when the database is corrected, wrong is not of the right length (eventually 0).

Proposition:

manual_correction(data, tc_dt, predicate=tc_dt %in% wrong & subjid  %in% c(128,196,17,73), correct=rep(NA, 4))

With predicate to be evaluated within data.

DanChaltiel commented 10 months ago
f = function(data, predicate, column, wrong, right, multiple=FALSE){
  x = data %>% filter(!!enquo(predicate) & {{column}}==wrong) %>% nrow()
  if(x==0) stop("already corrected")
  if(x>1 && !multiple) stop("More than 1 row corrected")

  data %>% 
    mutate(
      {{column}} := ifelse(!!enquo(predicate) & {{column}}==wrong, right, {{column}})
    )
}
df = iris %>% f(Species=="virginica" & Sepal.Width==3.6, column=Sepal.Length, wrong=7.2, right=999)
df = iris %>% f(Species=="virginica" & Sepal.Width==3.6, column=Sepal.Length, wrong=998, right=999)