Build a query that counts the number of nulls in each row.

count_null_cols(source, cols, count)

Arguments

source

incoming rel_op tree or data.frame.

cols

character, columns to track

count

character, column to write count in.

Value

rel_op node or data.frame (depending on input).

See also

Examples

# WARNING: example tries to change rquery.rquery_db_executor option to RSQLite and back. if (requireNamespace("DBI", quietly = TRUE) && requireNamespace("RSQLite", quietly = TRUE)) { my_db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") RSQLite::initExtension(my_db) old_o <- options(list("rquery.rquery_db_executor" = list(db = my_db))) d <- rq_copy_to(my_db, 'd', data.frame(AUC = c(0.6, 0.5, NA), R2 = c(1.0, 0.9, NA))) op_tree <- d %.>% count_null_cols(., c("AUC", "R2"), "nnull") cat(format(op_tree)) sql <- to_sql(op_tree, my_db) cat(sql) print(DBI::dbGetQuery(my_db, sql)) # ad-hoc mode data.frame(AUC=c(1,NA,0.5), R2=c(NA,1,0)) %.>% op_tree %.>% print(.) # cleanup options(old_o) DBI::dbDisconnect(my_db) }
#> mk_td("d", c( #> "AUC", #> "R2")) %.>% #> sql_node(., #> count_null_cols(AUC, R2)) #> SELECT #> `R2` AS `R2`, #> ( CASE WHEN ( `AUC` IS NULL ) THEN 1 ELSE 0 END ) + ( CASE WHEN ( `R2` IS NULL ) THEN 1 ELSE 0 END ) AS `nnull`, #> `AUC` AS `AUC` #> FROM ( #> SELECT #> `AUC`, #> `R2` #> FROM #> `d` #> ) tsql_73060327164569251995_0000000000 #> R2 nnull AUC #> 1 1.0 0 0.6 #> 2 0.9 0 0.5 #> 3 NA 2 NA #> R2 nnull AUC #> 1 NA 1 1.0 #> 2 1 1 NA #> 3 0 0 0.5