Factors with forcats : : CHEAT SHEET - GitHub

[Pages:1]Factors with forcats : : CHEAT SHEET

The forcats package provides tools for working with factors, which are R's data structure for categorical data.

Factors

stored

displayed

R represents categorical data with factors. A factor is an integer vector with a levels attribute that stores a set of mappings between

integer vector

levels

1 1= a

3 2

2= b 3= c

1

a 1= a

c b

2= b 3= c

a

integers and categorical values. When you view a factor, R

displays not the integers, but the levels associated with them.

=

a

a 1= a

c b

c b

2= b 3= c

a

a

a 1= a

a

c b

2= b 3= c

b c

a

Inspect Factors

a 1= a

fn

c b

2= b 3= c

a2 b1

a

c1

a 1= a b 2= b a

a 1= a b 2= b

fct_count(f, sort = FALSE, prop = FALSE) Count the number of values with each level. fct_count(f)

fct_match(f, lvls) Check for lvls in f. fct_match(f, "a")

fct_unique(f) Return the unique values, removing duplicates. fct_unique(f)

Change the order of levels

a 1= a

c b

2= b 3= c

a

c 1= a c 2= c a

a 1= b

c b

2= c 3= a

a

c 1= c c 2= a a

fct_relevel(.f, ..., a er = 0L) Manually reorder factor levels. fct_relevel(f, c("b", "c", "a"))

fct_infreq(f, ordered = NA) Reorder levels by the frequency in which they appear in the data (highest frequency first). Also fct_inseq(). f3 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download