relocate
In [1]:
Copied!
# https://dplyr.tidyverse.org/reference/relocate.html
%run nb_helpers.py
from datar.all import *
nb_header(relocate)
# https://dplyr.tidyverse.org/reference/relocate.html
%run nb_helpers.py
from datar.all import *
nb_header(relocate)
Try this notebook on binder.
★ relocate¶
change column positions¶
See original API
https://dplyr.tidyverse.org/reference/relocate.html
Args:¶
_data
: A data frame
*args
: and
**kwargs
: Columns to rename and move
_before
: and
_after
: Destination. Supplying neither will move columns to
the left-hand side; specifying both is an error.
Returns:¶
An object of the same type as .data. The output has the following
properties:
- Rows are not affected.
- The same columns appear in the output, but (usually) in a
different place.
- Data frame attributes are preserved. - Groups are not affected
In [2]:
Copied!
df = tibble(a=1, b=1, c=1, d='a', e='a', f='a')
df
df = tibble(a=1, b=1, c=1, d='a', e='a', f='a')
df
Out[2]:
a | b | c | d | e | f | |
---|---|---|---|---|---|---|
<int64> | <int64> | <int64> | <object> | <object> | <object> | |
0 | 1 | 1 | 1 | a | a | a |
In [3]:
Copied!
df >> relocate(f.f)
df >> relocate(f.f)
Out[3]:
f | a | b | c | d | e | |
---|---|---|---|---|---|---|
<object> | <int64> | <int64> | <int64> | <object> | <object> | |
0 | a | 1 | 1 | 1 | a | a |
In [4]:
Copied!
df >> relocate(f.a, _after = f.c)
df >> relocate(f.a, _after = f.c)
Out[4]:
b | c | a | d | e | f | |
---|---|---|---|---|---|---|
<int64> | <int64> | <int64> | <object> | <object> | <object> | |
0 | 1 | 1 | 1 | a | a | a |
In [5]:
Copied!
df >> relocate(f.f, _before = f.b)
df >> relocate(f.f, _before = f.b)
Out[5]:
a | f | b | c | d | e | |
---|---|---|---|---|---|---|
<int64> | <object> | <int64> | <int64> | <object> | <object> | |
0 | 1 | a | 1 | 1 | a | a |
In [6]:
Copied!
df >> relocate(f.a, _after = last_col())
df >> relocate(f.a, _after = last_col())
Out[6]:
b | c | d | e | f | a | |
---|---|---|---|---|---|---|
<int64> | <int64> | <object> | <object> | <object> | <int64> | |
0 | 1 | 1 | a | a | a | 1 |
In [7]:
Copied!
df >> relocate(ff=f.f)
df >> relocate(ff=f.f)
Out[7]:
ff | a | b | c | d | e | |
---|---|---|---|---|---|---|
<object> | <int64> | <int64> | <int64> | <object> | <object> | |
0 | a | 1 | 1 | 1 | a | a |
In [8]:
Copied!
df >> relocate(where(is_character))
df >> relocate(where(is_character))
Out[8]:
d | e | f | a | b | c | |
---|---|---|---|---|---|---|
<object> | <object> | <object> | <int64> | <int64> | <int64> | |
0 | a | a | a | 1 | 1 | 1 |
In [9]:
Copied!
df >> relocate(where(is_numeric), _after = last_col())
df >> relocate(where(is_numeric), _after = last_col())
Out[9]:
d | e | f | a | b | c | |
---|---|---|---|---|---|---|
<object> | <object> | <object> | <int64> | <int64> | <int64> | |
0 | a | a | a | 1 | 1 | 1 |
In [10]:
Copied!
df >> relocate(any_of(c("a", "e", "i", "o", "u")))
df >> relocate(any_of(c("a", "e", "i", "o", "u")))
Out[10]:
a | e | b | c | d | f | |
---|---|---|---|---|---|---|
<int64> | <object> | <int64> | <int64> | <object> | <object> | |
0 | 1 | a | 1 | 1 | a | a |
In [11]:
Copied!
df2 = tibble(a=1, b='a', c=1, d='a')
df2
df2 = tibble(a=1, b='a', c=1, d='a')
df2
Out[11]:
a | b | c | d | |
---|---|---|---|---|
<int64> | <object> | <int64> | <object> | |
0 | 1 | a | 1 | a |
In [12]:
Copied!
df2 >> relocate(where(is_numeric), _after = where(is_character))
df2 >> relocate(where(is_numeric), _after = where(is_character))
Out[12]:
b | d | a | c | |
---|---|---|---|---|
<object> | <object> | <int64> | <int64> | |
0 | a | a | 1 | 1 |
In [13]:
Copied!
df2 >> relocate(where(is_numeric), _before = where(is_character))
df2 >> relocate(where(is_numeric), _before = where(is_character))
Out[13]:
a | c | b | d | |
---|---|---|---|---|
<int64> | <int64> | <object> | <object> | |
0 | 1 | 1 | a | a |
In [14]:
Copied!
df2 >> relocate(f.d, _after=1)
df2 >> relocate(f.d, _after=1)
Out[14]:
a | b | d | c | |
---|---|---|---|---|
<int64> | <object> | <object> | <int64> | |
0 | 1 | a | a | 1 |