Replace NA/NULL is specified columns with the given replacement value.
null_replace(src, cols, value, ..., note_col = NULL, env = parent.frame())
relop or data.frame data source.
character, columns to work on.
scalar, value to write.
force later arguments to bind by name.
character, if not NULL record number of columns altered per-row in this column.
environment to look to.
null_replace node or data.frame.
if (requireNamespace("DBI", quietly = TRUE) && requireNamespace("RSQLite", quietly = TRUE)) {
my_db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
d1 <- rq_copy_to(my_db, 'd1',
data.frame(A = c(NA, 2, 3, NA), B = c(3, NA, 4, NA)))
optree <- null_replace(d1, qc(A, B),
0.0, note_col = "alterations")
cat(format(optree))
sql <- to_sql(optree, my_db)
cat(sql)
print(DBI::dbGetQuery(my_db, sql))
DBI::dbDisconnect(my_db)
}
#> mk_td("d1", c(
#> "A",
#> "B")) %.>%
#> null_replace(.; A,
#> B: 0; alterations)
#> SELECT
#> CASE WHEN `tsql_97660253230431739583_0000000000`.`A` IS NULL THEN 0 ELSE `tsql_97660253230431739583_0000000000`.`A` END AS `A`,
#> CASE WHEN `tsql_97660253230431739583_0000000000`.`B` IS NULL THEN 0 ELSE `tsql_97660253230431739583_0000000000`.`B` END AS `B`,
#> 0 +
#> ( CASE WHEN `tsql_97660253230431739583_0000000000`.`A` IS NULL THEN 1 ELSE 0 END ) +
#> ( CASE WHEN `tsql_97660253230431739583_0000000000`.`B` IS NULL THEN 1 ELSE 0 END ) AS `alterations`
#>
#> FROM (
#> SELECT
#> `A`,
#> `B`
#> FROM
#> `d1`
#> ) tsql_97660253230431739583_0000000000
#> A B alterations
#> 1 0 3 1
#> 2 2 0 1
#> 3 3 4 0
#> 4 0 0 2