complete copy/paste from forcats and haven

This commit is contained in:
Andreas Gammelgaard Damsbo 2024-11-20 14:31:01 +01:00
commit 18544ddcfe
No known key found for this signature in database
8 changed files with 216 additions and 55 deletions

View file

@ -2,6 +2,7 @@
% Please edit documentation in R/as_factor.R
\name{as_factor}
\alias{as_factor}
\alias{as_factor.factor}
\alias{as_factor.logical}
\alias{as_factor.numeric}
\alias{as_factor.character}
@ -11,20 +12,43 @@
\usage{
as_factor(x, ...)
\method{as_factor}{factor}(x, ...)
\method{as_factor}{logical}(x, ...)
\method{as_factor}{numeric}(x, ...)
\method{as_factor}{character}(x, ...)
\method{as_factor}{haven_labelled}(x, ...)
\method{as_factor}{haven_labelled}(
x,
levels = c("default", "labels", "values", "both"),
ordered = FALSE,
...
)
\method{as_factor}{labelled}(x, ...)
\method{as_factor}{labelled}(
x,
levels = c("default", "labels", "values", "both"),
ordered = FALSE,
...
)
}
\arguments{
\item{x}{Object to coerce to a factor.}
\item{...}{Other arguments passed down to method.}
\item{levels}{How to create the levels of the generated factor:
* "default": uses labels where available, otherwise the values.
Labels are sorted by value.
* "both": like "default", but pastes together the level and value
* "label": use only the labels; unlabelled values become `NA`
* "values": use only the values}
\item{ordered}{If `TRUE` create an ordered (ordinal) factor, if
`FALSE` (the default) create a regular (nominal) factor.}
}
\description{
This extends [forcats::as_factor()] as well as [haven::as_factor()], by appending
@ -33,15 +57,15 @@ ta loss in case of rich formatted and labelled data.
}
\details{
Please refer to parent functions for extended documentation.
To avoid redundancy calls and errors, functions are copy-pasted here
}
\examples{
# will preserve all attributes but class
\dontrun{
# will preserve all attributes
c(1, 4, 3, "A", 7, 8, 1) |> as_factor()
structure(c(1, 2, 3, 2, 10, 9),
labels = c(Unknown = 9, Refused = 10)
) |>
as_factor()
as_factor() |> dput()
structure(c(1, 2, 3, 2, 10, 9),
labels = c(Unknown = 9, Refused = 10),
@ -49,4 +73,3 @@ structure(c(1, 2, 3, 2, 10, 9),
) |>
as_factor()
}
}

View file

@ -16,9 +16,9 @@ numeric vector
Allows conversion of factor to numeric values preserving original levels
}
\examples{
\dontrun{
c(1, 4, 3, "A", 7, 8, 1) |>
as_factor() |> fct2num()
as_factor() |>
fct2num()
structure(c(1, 2, 3, 2, 10, 9),
labels = c(Unknown = 9, Refused = 10),
@ -28,9 +28,21 @@ structure(c(1, 2, 3, 2, 10, 9),
fct2num()
structure(c(1, 2, 3, 2, 10, 9),
labels = c(Unknown = 9, Refused = 10)
labels = c(Unknown = 9, Refused = 10),
class = "labelled"
) |>
as_factor() |>
fct2num()
}
# Outlier with labels, but no class of origin, handled like numeric vector
# structure(c(1, 2, 3, 2, 10, 9),
# labels = c(Unknown = 9, Refused = 10)
# ) |>
# as_factor() |>
# fct2num()
v <- sample(6:19,20,TRUE) |> factor()
dput(v)
named_levels(v)
fct2num(v)
}

View file

@ -19,7 +19,7 @@ Extract attribute. Returns NA if none
}
\examples{
attr(mtcars$mpg, "label") <- "testing"
do.call(c,sapply(mtcars, get_attr))
do.call(c, sapply(mtcars, get_attr))
\dontrun{
mtcars |>
numchar2fct(numeric.threshold = 6) |>

View file

@ -27,6 +27,8 @@ Get named vector of factor levels and values
structure(c(1, 2, 3, 2, 10, 9),
labels = c(Unknown = 9, Refused = 10),
class = "haven_labelled"
) |> as_factor() |> named_levels()
) |>
as_factor() |>
named_levels()
}
}