
Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Os melhores documentos à venda: Trabalhos de alunos formados
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Comunidade
Peça ajuda à comunidade e tire suas dúvidas relacionadas ao estudo
Descubra as melhores universidades em seu país de acordo com os usuários da Docsity
Guias grátis
Baixe gratuitamente nossos guias de estudo, métodos para diminuir a ansiedade, dicas de TCC preparadas pelos professores da Docsity
Saiba como utilizar o pacote forcats em r para criar, manipular e reordenar fatores, uma estrutura de dados para dados categoricais. Aprenda a combinar, inspecionar, reordenar e transformar fatores com várias funções fornecidas pelo pacote.
O que você vai aprender
Tipologia: Teses (TCC)
1 / 1
Esta página não é visível na pré-visualização
Não perca as partes importantes!
1 = 2 = 1 = 2 = 3 = (^) x 1 = 2 = 3 = 1 = 2 = x 1 = 2 = 3 = NA 1 = 2 =
1 = 2 = 1 = 2 = 3 = x 1 = 2 = 3 = 1 = 2 = 3 =
1 = 2 = 1 = 2 = 1 = 2 = 3 = 1 = 2 = 3 = 2 a c
R represents categorical data with factors. A factor is an integer vector with a levels attribute that stores a set of mappings between integers and categorical values. When you view a factor, R displays not the integers, but the values associated with them. fct_c (…) Combine factors with different levels. f1 <- factor(c("a", "c")) f2 <- factor(c("b", "a")) fct_c(f1, f2) fct_unify (fs, levels = lvls_union(fs)) Standardize levels across a list of factors. fct_unify(list(f2, f1))
RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at forcats.tidyverse.org • Diagrams inspired by @LVaudor! • forcats 0.3.0 • Updated: 2019-
Create a factor with factor() factor (x = character(), levels, labels = levels, exclude = NA, ordered = is.ordered(x), nmax = NA) Convert a vector to a factor. Also as_factor. f <- factor(c("a", "c", "b", "a"), levels = c("a", "b", "c")) Return its levels with levels() levels (x) Return/set the levels of a factor. levels(f); levels(f) <- c("x","y","z") Use unclass() to see its structure
fct_drop (f, only) Drop unused levels. f5 <- factor(c("a","b"),c("a","b","x")) f6 <- fct_drop(f5) fct_expand (f, …) Add levels to a factor. fct_expand(f6, "x") fct_explicit_na (f, na_level="(Missing)") Assigns a level to NAs to ensure they appear in plots, etc. fct_explicit_na(factor(c("a", "b", NA))) fct_count (f, sort = FALSE) Count the number of values with each level. fct_count(f) fct_unique (f) Return the unique values, removing duplicates. fct_unique(f) fct_recode (.f, ...) Manually change levels. Also fct_relabel which obeys purrr::map syntax to apply a function or expression to each level. fct_recode(f, v = "a", x = "b", z = "c") fct_relabel(f, ~ paste0("x", .x)) fct_anon (f, prefix = "")) Anonymize levels with random integers. fct_anon(f) fct_collapse (.f, ...) Collapse levels into manually defined groups. fct_collapse(f, x = c("a", "b")) fct_lump (f, n, prop, w = NULL, other_level = "Other", ties.method = c("min", "average", "first", "last", "random", "max")) Lump together least/most common levels into a single level. Also fct_lump_min. fct_lump(f, n = 1) fct_other (f, keep, drop, other_level = "Other") Replace levels with "other." fct_other(f, keep = c("a", "b"))
1 = 2 = 3 = integer vector levels 1 = 2 = 3 = stored displayed 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 3 = f n 2 1 1 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 1 = 2 =
1 = 2 2 = 1 3 = 3 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 3 = Other Other 1 = 2 = 3 = Other Other 1 = 2 = Other 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 3 =
fct_relevel (.f, ..., after = 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). f3 <- factor(c("c", "c", "a")) fct_infreq(f3) fct_inorder (f, ordered = NA) Reorder levels by order in which they appear in the data. fct_inorder(f2) fct_rev (f) Reverse level order. f4 <- factor(c("a","b","c")) fct_rev(f4) fct_shift (f) Shift levels to left or right, wrapping around end. fct_shift(f4) fct_shuffle (f, n = 1L) Randomly permute order of factor levels. fct_shuffle(f4) fct_reorder (.f, .x, .fun=median, ..., .desc = FALSE) Reorder levels by their relationship with another variable. boxplot(data = iris, Sepal.Width ~ fct_reorder(Species, Sepal.Width)) fct_reorder2 (.f, .x, .y, .fun = last2, ..., .desc = TRUE) Reorder levels by their final values when plotted with two other variables. ggplot(data = iris, aes(Sepal.Width, Sepal.Length, color = fct_reorder2(Species, Sepal.Width, Sepal.Length))) + geom_smooth() 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 1 = 2 = 1 = 2 = 3 = 1 = 2 = 3 = 1 = 2 = 1 = 2 = 1 = 2 = 3 = 1 = 2 = 3 = b c a
a b c
a b c a a b
b a b a b a x b a b a b a b a c b a b a c a b a c b a c b a c x v z c b a c c b a c a b a a b a c b a c b a c b a c b c a a c b c a b b a c c b a b a c c b a b a c b a c b a c b a c b a c b a c b a c b a c c a b c a a b b a b a c b a c a c