rowwise
In [1]:
Copied!
# https://dplyr.tidyverse.org/reference/rowwise.html
%run nb_helpers.py
from datar.all import *
nb_header(rowwise)
# https://dplyr.tidyverse.org/reference/rowwise.html
%run nb_helpers.py
from datar.all import *
nb_header(rowwise)
In [2]:
Copied!
df = tibble(x=runif(6), y=runif(6), z=runif(6))
df >> rowwise() >> mutate(m=mean(c_across(c(f.x, f.y, f.z))))
df = tibble(x=runif(6), y=runif(6), z=runif(6))
df >> rowwise() >> mutate(m=mean(c_across(c(f.x, f.y, f.z))))
Out[2]:
x | y | z | m | |
---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | |
0 | 0.740427 | 0.722062 | 0.796442 | 0.752977 |
1 | 0.420291 | 0.016988 | 0.681690 | 0.372989 |
2 | 0.983908 | 0.507227 | 0.508663 | 0.666599 |
3 | 0.059662 | 0.483942 | 0.047045 | 0.196883 |
4 | 0.288083 | 0.735485 | 0.868933 | 0.630834 |
5 | 0.184138 | 0.502597 | 0.424734 | 0.370489 |
TibbleRowwise: (n=6)
In [3]:
Copied!
df >> rowwise() >> mutate(m=mean(c_across(f[f.x:])))
df >> rowwise() >> mutate(m=mean(c_across(f[f.x:])))
Out[3]:
x | y | z | m | |
---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | |
0 | 0.740427 | 0.722062 | 0.796442 | 0.752977 |
1 | 0.420291 | 0.016988 | 0.681690 | 0.372989 |
2 | 0.983908 | 0.507227 | 0.508663 | 0.666599 |
3 | 0.059662 | 0.483942 | 0.047045 | 0.196883 |
4 | 0.288083 | 0.735485 | 0.868933 | 0.630834 |
5 | 0.184138 | 0.502597 | 0.424734 | 0.370489 |
TibbleRowwise: (n=6)
In [4]:
Copied!
df >> rowwise() >> mutate(m=min(c_across([f.x, f.y, f.z])))
df >> rowwise() >> mutate(m=min(c_across([f.x, f.y, f.z])))
Out[4]:
x | y | z | m | |
---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | |
0 | 0.740427 | 0.722062 | 0.796442 | 0.722062 |
1 | 0.420291 | 0.016988 | 0.681690 | 0.016988 |
2 | 0.983908 | 0.507227 | 0.508663 | 0.507227 |
3 | 0.059662 | 0.483942 | 0.047045 | 0.047045 |
4 | 0.288083 | 0.735485 | 0.868933 | 0.288083 |
5 | 0.184138 | 0.502597 | 0.424734 | 0.184138 |
TibbleRowwise: (n=6)
In [5]:
Copied!
df >> rowwise() >> mutate(m=min(c_across(f[f.x:])))
df >> rowwise() >> mutate(m=min(c_across(f[f.x:])))
Out[5]:
x | y | z | m | |
---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | |
0 | 0.740427 | 0.722062 | 0.796442 | 0.722062 |
1 | 0.420291 | 0.016988 | 0.681690 | 0.016988 |
2 | 0.983908 | 0.507227 | 0.508663 | 0.507227 |
3 | 0.059662 | 0.483942 | 0.047045 | 0.047045 |
4 | 0.288083 | 0.735485 | 0.868933 | 0.288083 |
5 | 0.184138 | 0.502597 | 0.424734 | 0.184138 |
TibbleRowwise: (n=6)
In [6]:
Copied!
df >> mutate(m = pmin(f.x, f.y, f.z))
df >> mutate(m = pmin(f.x, f.y, f.z))
Out[6]:
x | y | z | m | |
---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | |
0 | 0.740427 | 0.722062 | 0.796442 | 0.722062 |
1 | 0.420291 | 0.016988 | 0.681690 | 0.016988 |
2 | 0.983908 | 0.507227 | 0.508663 | 0.507227 |
3 | 0.059662 | 0.483942 | 0.047045 | 0.047045 |
4 | 0.288083 | 0.735485 | 0.868933 | 0.288083 |
5 | 0.184138 | 0.502597 | 0.424734 | 0.184138 |
In [7]:
Copied!
params = tibble(
sim=[1, 2, 3],
n=[1, 2, 3],
mean=[1, 2, 1],
sd=[1, 4, 2]
)
params >> rowwise(f.sim) >> mutate(z=rnorm(f.n, f.mean, f.sd))
params = tibble(
sim=[1, 2, 3],
n=[1, 2, 3],
mean=[1, 2, 1],
sd=[1, 4, 2]
)
params >> rowwise(f.sim) >> mutate(z=rnorm(f.n, f.mean, f.sd))
Out[7]:
sim | n | mean | sd | z | |
---|---|---|---|---|---|
<int64> | <int64> | <int64> | <int64> | <object> | |
0 | 1 | 1 | 1 | 1 | [1.8805605045050253] |
1 | 2 | 2 | 2 | 4 | [-2.39100601732882, 2.6882535131773917] |
2 | 3 | 3 | 1 | 2 | [-0.8909366235397611, 2.2500642263497337, 4.00... |
TibbleRowwise: sim (n=3)
In [ ]:
Copied!