A generic function and several instances for creating factors from other sorts of data. The primary use case is for vectors that contain few unique values and might be better considered as factors. When applied to a data frame, this is applied to each variable in the data frame.
factorize(x, ...)
# S3 method for default
factorize(x, ...)
# S3 method for numeric
factorize(x, max.levels = 5L, ...)
# S3 method for character
factorize(x, max.levels = 5L, ...)
# S3 method for data.frame
factorize(x, max.levels = 5L, ...)
factorise(x, ...)
an object
additional arguments (currently ignored)
an integer. Only convert if the number of unique values is no
more than max.levels
.
data(KidsFeet, package="mosaicData")
str(KidsFeet)
#> 'data.frame': 39 obs. of 8 variables:
#> $ name : Factor w/ 36 levels "Abby","Alisha",..: 10 24 36 20 23 34 13 4 14 8 ...
#> $ birthmonth: int 5 10 12 1 2 3 2 6 5 9 ...
#> $ birthyear : int 88 87 87 88 88 88 88 88 88 88 ...
#> $ length : num 24.4 25.4 24.5 25.2 25.1 25.7 26.1 23 23.6 22.9 ...
#> $ width : num 8.4 8.8 9.7 9.8 8.9 9.7 9.6 8.8 9.3 8.8 ...
#> $ sex : Factor w/ 2 levels "B","G": 1 1 1 1 1 1 1 2 2 1 ...
#> $ biggerfoot: Factor w/ 2 levels "L","R": 1 1 2 1 1 2 1 1 2 2 ...
#> $ domhand : Factor w/ 2 levels "L","R": 2 1 2 2 2 2 2 2 2 1 ...
factorize(KidsFeet$birthyear)
#> [1] 88 87 87 88 88 88 88 88 88 88 87 88 87 88 87 88 87 88 88 88 88 88 88 88 88
#> [26] 88 88 87 88 88 88 88 88 88 88 88 88 88 88
#> Levels: 87 88
str(factorize(KidsFeet))
#> 'data.frame': 39 obs. of 8 variables:
#> $ name : Factor w/ 36 levels "Abby","Alisha",..: 10 24 36 20 23 34 13 4 14 8 ...
#> $ birthmonth: int 5 10 12 1 2 3 2 6 5 9 ...
#> $ birthyear : Factor w/ 2 levels "87","88": 2 1 1 2 2 2 2 2 2 2 ...
#> $ length : num 24.4 25.4 24.5 25.2 25.1 25.7 26.1 23 23.6 22.9 ...
#> $ width : num 8.4 8.8 9.7 9.8 8.9 9.7 9.6 8.8 9.3 8.8 ...
#> $ sex : Factor w/ 2 levels "B","G": 1 1 1 1 1 1 1 2 2 1 ...
#> $ biggerfoot: Factor w/ 2 levels "L","R": 1 1 2 1 1 2 1 1 2 2 ...
#> $ domhand : Factor w/ 2 levels "L","R": 2 1 2 2 2 2 2 2 2 1 ...
# alternative spelling
str(factorise(KidsFeet))
#> 'data.frame': 39 obs. of 8 variables:
#> $ name : Factor w/ 36 levels "Abby","Alisha",..: 10 24 36 20 23 34 13 4 14 8 ...
#> $ birthmonth: int 5 10 12 1 2 3 2 6 5 9 ...
#> $ birthyear : Factor w/ 2 levels "87","88": 2 1 1 2 2 2 2 2 2 2 ...
#> $ length : num 24.4 25.4 24.5 25.2 25.1 25.7 26.1 23 23.6 22.9 ...
#> $ width : num 8.4 8.8 9.7 9.8 8.9 9.7 9.6 8.8 9.3 8.8 ...
#> $ sex : Factor w/ 2 levels "B","G": 1 1 1 1 1 1 1 2 2 1 ...
#> $ biggerfoot: Factor w/ 2 levels "L","R": 1 1 2 1 1 2 1 1 2 2 ...
#> $ domhand : Factor w/ 2 levels "L","R": 2 1 2 2 2 2 2 2 2 1 ...