Quantcast
Channel: wrapr – Win-Vector Blog
Viewing all articles
Browse latest Browse all 38

coalesce with wrapr

$
0
0

coalesce is a classic useful SQL operator that picks the first non-NULL value in a sequence of values.

We thought we would share a nice version of it for picking non-NA R with convenient operator infix notation wrapr::coalesce(). Here is a short example of it in action:

library("wrapr")

NA %?% 0

# [1] 0

A more substantial application is the following.

library("wrapr")

d <- wrapr::build_frame(
  "more_precise_sensor", "cheaper_back_up_sensor" |
  0.31                 , NA                       |
  0.41                 , 0.5                      |
  NA                   , 0.5                      )

print(d)

#   more_precise_sensor cheaper_back_up_sensor
# 1                0.31                   NA
# 2                0.41                   0.50
# 3                  NA                   0.50

d$measurement <- 
   d$more_precise_sensor %?% d$cheaper_back_up_sensor

print(d)

#   more_precise_sensor cheaper_back_up_sensor measurement
# 1                0.31                   NA          0.31
# 2                0.41                   0.50        0.41
# 3                  NA                   0.50        0.50

Viewing all articles
Browse latest Browse all 38

Trending Articles