datar.apis.forcats
datar.apis.forcats
fct_anon
(
_f
,prefix
)
(Any) — Anonymise factor levels</>fct_c
(
*fs
)
(Any) — Concatenate factors, combining levels</>fct_collapse
(
_f
,other_level
,**kwargs
)
(Any) — Collapse factor levels into manually defined groups</>fct_count
(
_f
,sort
,prop
)
(Any) — Count entries in a factor</>fct_cross
(
*fs
,sep
,keep_empty
)
(Any) — Combine levels from two or more factors to create a new factor</>fct_drop
(
_f
,only
)
(Any) — Drop unused levels</>fct_expand
(
_f
,*additional_levels
)
(Any) — Add additional levels to a factor</>fct_explicit_na
(
_f
,na_level
)
(Any) — Make missing values explicit</>fct_infreq
(
_f
,ordered
)
(Any) — Reorder factor levels by frequency</>fct_inorder
(
_f
,ordered
)
(Any) — Reorder factor levels by first appearance</>fct_inseq
(
_f
,ordered
)
(Any) — Reorder factor levels by sequence</>fct_lump
(
_f
,n
,prop
,w
,other_level
,ties_method
)
(Any) — Lump together factor levels into "other"</>fct_lump_lowfreq
(
_f
,other_level
)
(Any) — lumps together the least frequent levels, ensuringthat "other" is still the smallest level. </>fct_lump_min
(
_f
,min_
,w
,other_level
)
(Any) — lumps levels that appear fewer thanmin_
times.</>fct_lump_n
(
_f
,n
,w
,other_level
)
(Any) — Lumps all levels except for then
most frequent.</>fct_lump_prop
(
_f
,prop
,w
,other_level
)
(Any) — Lumps levels that appear in fewerprop * n
times.</>fct_match
(
_f
,lvls
)
(Any) — Test for presence of levels in a factor</>fct_other
(
_f
,keep
,drop
,other_level
)
(Any) — Replace levels with "other"</>fct_recode
(
_f
,*args
,**kwargs
)
(Any) — Change factor levels by hand</>fct_relabel
(
_f
,_fun
,*args
,**kwargs
)
(Any) — Automatically relabel factor levels, collapse as necessary</>fct_relevel
(
_f
,*lvls
,after
)
(Any) — Reorder factor levels by hand</>fct_reorder
(
_f
,_x
,*args
,_fun
,_desc
,**kwargs
)
(Any) — Reorder factor levels by a function (default: median)</>fct_reorder2
(
_f
,_x
,*args
,_fun
,_desc
,**kwargs
)
(Any) — Reorder factor levels by a function (default:last2
)</>fct_rev
(
_f
)
(Any) — Reverse the order of the levels of a factor</>fct_shift
(
_f
,n
)
(Any) — Shift the levels of a factor</>fct_shuffle
(
_f
)
(Any) — Shuffle the levels of a factor</>fct_unify
(
fs
,levels
)
(Any) — Unify the levels in a list of factors</>fct_unique
(
_f
)
(Any) — Unique values of a factor</>first2
(
_x
,_y
)
(Any) — Find the first element of_y
ordered by_x
</>last2
(
_x
,_y
)
(Any) — Find the last element of_y
ordered by_x
</>lvls_expand
(
_f
,new_levels
)
(Any) — Expands the set of levels; the new levels mustinclude the old levels. </>lvls_reorder
(
_f
,idx
,ordered
)
(Any) — Leaves values of a factor as they are, but changes the order bygiven indices </>lvls_revalue
(
_f
,new_levels
)
(Any) — changes the values of existing levels; there mustbe one new level for each old level </>lvls_union
(
fs
)
(Any) — Find all levels in a list of factors</>
datar.apis.forcats.
fct_relevel
(
_f
, *lvls
, after=None
)
Reorder factor levels by hand
_f
— A factor (categoriccal), or a string vector*lvls
— Either a function (thenlen(lvls)
should equal to1
) orthe new levels. A function will be called with the current levels as input, and the return value (which must be a character vector) will be used to relevel the factor. Any levels not mentioned will be left in their existing order, by default after the explicitly mentioned levels.after
(int, optional) — Where should the new values be placed?
The factor with levels replaced
datar.apis.forcats.
fct_inorder
(
_f
, ordered=None
)
Reorder factor levels by first appearance
_f
— A factorordered
(bool, optional) — A logical which determines the "ordered" status of theoutput factor.
The factor with levels reordered
datar.apis.forcats.
fct_infreq
(
_f
, ordered=None
)
Reorder factor levels by frequency
_f
— A factorordered
(bool, optional) — A logical which determines the "ordered" status of theoutput factor.
The factor with levels reordered
datar.apis.forcats.
fct_inseq
(
_f
, ordered=None
)
Reorder factor levels by sequence
_f
— A factorordered
(bool, optional) — A logical which determines the "ordered" status of theoutput factor.
The factor with levels reordered
datar.apis.forcats.
fct_reorder
(
_f
, _x
, *args
, _fun=None
, _desc=False
, **kwargs
)
Reorder factor levels by a function (default: median)
_f
— A factor_x
— The data to be used to reorder the factor*args
— Extra arguments to be passed to_fun
_fun
(optional) — A function to be used to reorder the factor_desc
(bool, optional) — IfTrue
, the factor will be reordered in descending order**kwargs
— Extra keyword arguments to be passed to_fun
The factor with levels reordered
datar.apis.forcats.
fct_reorder2
(
_f
, _x
, *args
, _fun=None
, _desc=False
, **kwargs
)
Reorder factor levels by a function (default: last2
)
_f
— A factor_x
— The data to be used to reorder the factor*args
— Extra arguments to be passed to_fun
_fun
(optional) — A function to be used to reorder the factor_desc
(bool, optional) — IfTrue
, the factor will be reordered in descending order**kwargs
— Extra keyword arguments to be passed to_fun
The factor with levels reordered
datar.apis.forcats.
fct_shuffle
(
_f
)
Shuffle the levels of a factor
_f
— A factor
The factor with levels shuffled
datar.apis.forcats.
fct_rev
(
_f
)
Reverse the order of the levels of a factor
_f
— A factor
The factor with levels reversed
datar.apis.forcats.
fct_shift
(
_f
, n=1
)
Shift the levels of a factor
_f
— A factorn
(int, optional) — The number of levels to shift
The factor with levels shifted
datar.apis.forcats.
first2
(
_x
, _y
)
Find the first element of _y
ordered by _x
_x
— The vector used to order_y
_y
— The vector to get the first element of
First element of _y
ordered by _x
datar.apis.forcats.
last2
(
_x
, _y
)
Find the last element of _y
ordered by _x
_x
— The vector used to order_y
_y
— The vector to get the last element of
Last element of _y
ordered by _x
datar.apis.forcats.
fct_anon
(
_f
, prefix=''
)
Anonymise factor levels
prefix
(str, optional) — A character prefix to insert in front of the random labels.f
— A factor.
The factor with levels anonymised
datar.apis.forcats.
fct_recode
(
_f
, *args
, **kwargs
)
Change factor levels by hand
_f
— A factor*args
— and**kwargs
— A sequence of named character vectors where the namegives the new level, and the value gives the old level. Levels not otherwise mentioned will be left as is. Levels can be removed by naming themNULL
. AsNULL/None
cannot be a name of keyword arguments, replacement has to be specified as a dict (i.e.fct_recode(x, {NULL: "apple"})
) If you want to replace multiple values with the same old value, use aset
/list
/numpy.ndarray
(i.e.fct_recode(x, fruit=["apple", "banana"])
). This is a safe way, sinceset
/list
/numpy.ndarray
is not hashable to be a level of a factor. Do NOT use atuple
, as it's hashable!
Note that the order of the name-value is in the reverse way asdplyr.recode()
anddplyr.recode_factor()
The factor recoded with given recodings
datar.apis.forcats.
fct_collapse
(
_f
, other_level=None
, **kwargs
)
Collapse factor levels into manually defined groups
_f
— A factorother_level
(optional) — Replace all levels not named inkwargs
.If not, don't collapse them.**kwargs
— The levels to collapse.Likename=[old_level, old_level1, ...]
. The old levels will be replaced withname
The factor with levels collapsed.
datar.apis.forcats.
fct_lump
(
_f
, n=None
, prop=None
, w=None
, other_level='Other'
, ties_method='min'
)
Lump together factor levels into "other"
n
(optional) — Positiven
preserves the most commonn
values.Negativen
preserves the least common-n
values. It there are ties, you will get at leastabs(n)
values.prop
(optional) — Positiveprop
lumps values which do not appear at leastprop
of the time. Negativeprop
lumps values that do not appear at most-prop
of the time.w
(optional) — An optional numeric vector giving weights for frequency ofeach value (not level) in f.other_level
(optional) — Value of level used for "other" values. Alwaysplaced at end of levels.f
— A factorties_method A character string specifying how ties are treated.
— One of:average
,first
,dense
,max
, andmin
.
The factor with levels lumped.
datar.apis.forcats.
fct_lump_min
(
_f
, min_
, w=None
, other_level='Other'
)
lumps levels that appear fewer than min_
times.
_f
— A factormin_
— Preserve levels that appear at leastmin_
number of times.w
(optional) — An optional numeric vector giving weights for frequency ofeach value (not level) in f.other_level
(optional) — Value of level used for "other" values. Alwaysplaced at end of levels.
The factor with levels lumped.
datar.apis.forcats.
fct_lump_prop
(
_f
, prop
, w=None
, other_level='Other'
)
Lumps levels that appear in fewer prop * n
times.
_f
— A factorprop
— Positiveprop
lumps values which do not appear at leastprop
of the time. Negativeprop
lumps values that do not appear at most-prop
of the time.w
(optional) — An optional numeric vector giving weights for frequency ofeach value (not level) in f.other_level
(optional) — Value of level used for "other" values. Alwaysplaced at end of levels.
The factor with levels lumped.
datar.apis.forcats.
fct_lump_n
(
_f
, n
, w=None
, other_level='Other'
)
Lumps all levels except for the n
most frequent.
n
— Positiven
preserves the most commonn
values.Negativen
preserves the least common-n
values. It there are ties, you will get at leastabs(n)
values.w
(optional) — An optional numeric vector giving weights for frequency ofeach value (not level) in f.other_level
(optional) — Value of level used for "other" values. Alwaysplaced at end of levels.f
— A factorties_method A character string specifying how ties are treated.
— One of:average
,first
,dense
,max
, andmin
.
The factor with levels lumped.
datar.apis.forcats.
fct_lump_lowfreq
(
_f
, other_level='Other'
)
lumps together the least frequent levels, ensuringthat "other" is still the smallest level.
other_level
(optional) — Value of level used for "other" values. Alwaysplaced at end of levels.f
— A factor
The factor with levels lumped.
datar.apis.forcats.
fct_other
(
_f
, keep=None
, drop=None
, other_level='Other'
)
Replace levels with "other"
_f
— A factorkeep
(optional) — anddrop
(optional) — Pick one ofkeep
anddrop
:- -
keep
will preserve listed levels, replacing all others with
other_level
. - -
drop
will replace listed levels withother_level
, keeping all
as is.
- -
other_level
(optional) — Value of level used for "other" values. Alwaysplaced at end of levels.
The factor with levels replaced.
datar.apis.forcats.
fct_relabel
(
_f
, _fun
, *args
, **kwargs
)
Automatically relabel factor levels, collapse as necessary
_f
— A factor_fun
— A function to be applied to each level. Must accept the oldlevels and return a character vector of the same length as its input.*args
— and**kwargs
— Addtional arguments to_fun
The factor with levels relabeled
datar.apis.forcats.
fct_expand
(
_f
, *additional_levels
)
Add additional levels to a factor
_f
— A factor*additional_levels
— Additional levels to add to the factor.Levels that already exist will be silently ignored.
The factor with levels expanded
datar.apis.forcats.
fct_explicit_na
(
_f
, na_level='(Missing)'
)
Make missing values explicit
This gives missing values an explicit factor level, ensuring that they appear in summaries and on plots.
_f
— A factorna_level
(optional) — Level to use for missing values.This is what NAs will be changed to.
The factor with explict na_levels
datar.apis.forcats.
fct_drop
(
_f
, only=None
)
Drop unused levels
_f
— A factoronly
(optional) — A character vector restricting the set of levels to be dropped.If supplied, only levels that have no entries and appear in this vector will be removed.
The factor with unused levels dropped
datar.apis.forcats.
fct_unify
(
fs
, levels=None
)
Unify the levels in a list of factors
fs
— A list of factorslevels
(optional) — Set of levels to apply to every factor. Default to unionof all factor levels
A list of factors with the levels expanded
datar.apis.forcats.
fct_c
(
*fs
)
Concatenate factors, combining levels
This is a useful ways of patching together factors from multiple sources that really should have the same levels but don't.
*fs
— factors to concatenate
The concatenated factor
datar.apis.forcats.
fct_cross
(
*fs
, sep=':'
, keep_empty=False
)
Combine levels from two or more factors to create a new factor
Computes a factor whose levels are all the combinations of the levels of the input factors.
*fs
— factors to crosssep
(str, optional) — A string to separate levelskeep_empty
(bool, optional) — If True, keep combinations with no observations as levels
The new factor
datar.apis.forcats.
fct_count
(
_f
, sort=False
, prop=False
)
Count entries in a factor
_f
— A factorsort
(bool, optional) — If True, sort the result so that the most common values float tothe topprop
(optional) — If True, compute the fraction of marginal table.
A data frame with columns f
, n
and p
, if prop is True
datar.apis.forcats.
fct_match
(
_f
, lvls
)
Test for presence of levels in a factor
Do any of lvls
occur in _f
?
_f
— A factorlvls
— A vector specifying levels to look for.
A logical factor
datar.apis.forcats.
fct_unique
(
_f
)
Unique values of a factor
_f
— A factor
The factor with the unique values in _f
datar.apis.forcats.
lvls_reorder
(
_f
, idx
, ordered=None
)
Leaves values of a factor as they are, but changes the order bygiven indices
idx
— A integer index, with one integer for each existing level.ordered
(bool, optional) — A logical which determines the "ordered" status of theoutput factor.None
preserves the existing status of the factor.f
— A factor (or character vector).new_levels
— A character vector of new levels.
The factor with levels reordered
datar.apis.forcats.
lvls_revalue
(
_f
, new_levels
)
changes the values of existing levels; there mustbe one new level for each old level
_f
— A factornew_levels
— A character vector of new levels.
The factor with the new levels
datar.apis.forcats.
lvls_expand
(
_f
, new_levels
)
Expands the set of levels; the new levels mustinclude the old levels.
_f
— A factornew_levels
— The new levels. Must include the old ones
The factor with the new levels
datar.apis.forcats.
lvls_union
(
fs
)
Find all levels in a list of factors
fs
— A list of factors
A list of all levels