coalesce
In [1]:
Copied!
# https://dplyr.tidyverse.org/reference/coalesce.html
%run nb_helpers.py
from datar.all import *
nb_header(coalesce)
# https://dplyr.tidyverse.org/reference/coalesce.html
%run nb_helpers.py
from datar.all import *
nb_header(coalesce)
In [2]:
Copied!
df = tibble(x=[5,4,3,NA,2,NA,1,NA])
df >> mutate(y=coalesce(f.x, 0)) >> pull(f.y)
df = tibble(x=[5,4,3,NA,2,NA,1,NA])
df >> mutate(y=coalesce(f.x, 0)) >> pull(f.y)
Out[2]:
0 5.0 1 4.0 2 3.0 3 0.0 4 2.0 5 0.0 6 1.0 7 0.0 Name: y, dtype: float64
In [3]:
Copied!
df = tibble(
y=[1,2,NA,NA,5],
z=[NA,NA,3,4,5]
)
df >> mutate(m=coalesce(f.y, f.z)) >> pull(f.m)
df = tibble(
y=[1,2,NA,NA,5],
z=[NA,NA,3,4,5]
)
df >> mutate(m=coalesce(f.y, f.z)) >> pull(f.m)
Out[3]:
0 1.0 1 2.0 2 3.0 3 4.0 4 5.0 Name: m, dtype: float64