forcats

Combine Rarely Used Factor Levels Across all Factors in Data Frame

筅森魡賤 提交于 2021-02-17 02:37:31
问题 I would like to combine factor levels that are used fewer than 5 times for each factor in a dataset containing many different factors. While I understand that the fct_lump() function in the forcats package can help me achieve this for a single factor, is there a function where I can apply the fct_lump() function over all the factors in my dataset? 回答1: We can check whether the column is factor with mutate_if and apply fct_lump library(dplyr) library(forcats) df1 %>% mutate_if(is.factor, fct

How to supply variable names as strings in fct_reorder within ggplot code?

江枫思渺然 提交于 2021-02-08 10:24:09
问题 I want to create the geom_col chart by supplying the variable names as strings. I know the aes_string does that in the ggplot code. However, I am not sure how to handle the variables supplied in the fct_reorder to order the bars. Here is my wip code. I want to convert this: mpg %>% ggplot() + geom_col(aes(x = cty, y = fct_reorder(drv, cty, .fun = mean, na.rm = T), fill = drv), width = 0.8) + theme_classic() into a function, chart_fun <- function(x, y) { mpg %>% ggplot() + geom_col(aes_string

how to reorder a factor in a dataframe with fct_reorder?

拥有回忆 提交于 2020-08-20 06:42:01
问题 Consider the following example > library(forcats) > library(dplyr) > > > dataframe <- data_frame(var = c(1,1,1,2,3,4), + var2 = c(10,9,8,7,6,5)) > dataframe # A tibble: 6 x 2 var var2 <dbl> <dbl> 1 1.00 10.0 2 1.00 9.00 3 1.00 8.00 4 2.00 7.00 5 3.00 6.00 6 4.00 5.00 I create a factor variable > dataframe <- dataframe %>% mutate(myfactor = factor(var)) > > dataframe$myfactor [1] 1 1 1 2 3 4 Levels: 1 2 3 4 I do not understand what is the correct syntax (and the logic) to reorder this factor

How to reorder a factor based on a subset (facets) of another variable, using forcats?

≯℡__Kan透↙ 提交于 2020-06-27 13:40:18
问题 forcats vignette states that The goal of the forcats package is to provide a suite of useful tools that solve common problems with factors And indeed one of the tools is to reorder factors by another variable, which is a very common use case in plotting data. I was trying to use forcats to accomplish this, but in the case of a faceted plot. That is, I want to reorder a factor by other variable, but using only a subset of the data. Here's a reprex: library(tidyverse) ggplot2::diamonds %>%

re-ordering factors according to a value using fct_reorder in R

血红的双手。 提交于 2020-03-19 05:25:29
问题 My data: structure(list(LoB = c("C", "C", "C", "A", "A", "B", "C", "A", "A", "C", "A", "B", "C", "B", "A", "C", "B", "A", "B", "C", "A", "B", "B", "A", "B", "C", "A", "B", "C", "B"), word = c("speed", "connection", "call", "bt", "reliable", "reliable", "reliable", "expensive", "cheaper", "uk", "customer", "customer", "customer", "network", "broadband", "broadband", "signal", "price", "price", "price", "poor", "poor", "ee", "service", "service", "service", "excellent", "excellent", "excellent"

re-ordering factors according to a value using fct_reorder in R

给你一囗甜甜゛ 提交于 2020-03-19 05:25:08
问题 My data: structure(list(LoB = c("C", "C", "C", "A", "A", "B", "C", "A", "A", "C", "A", "B", "C", "B", "A", "C", "B", "A", "B", "C", "A", "B", "B", "A", "B", "C", "A", "B", "C", "B"), word = c("speed", "connection", "call", "bt", "reliable", "reliable", "reliable", "expensive", "cheaper", "uk", "customer", "customer", "customer", "network", "broadband", "broadband", "signal", "price", "price", "price", "poor", "poor", "ee", "service", "service", "service", "excellent", "excellent", "excellent"

Difference of fct_reorder and reorder

↘锁芯ラ 提交于 2020-01-15 10:08:19
问题 This is an exmaple of fct_reorder boxplot(Sepal.Width ~ fct_reorder(Species, Sepal.Width, .desc = TRUE), data = iris) This code is identical with boxplot(Sepal.Width ~ reorder(Species, -Sepal.Width), data = iris) What is the better point fct_reorder() than reorder() ? 来源: https://stackoverflow.com/questions/51378506/difference-of-fct-reorder-and-reorder

Passing string variable to forcats::fct_reorder

笑着哭i 提交于 2019-12-20 06:12:28
问题 Any idea how to pass a string sorting variable to fct_reorder ? require(dplyr) require(forcats) require(ggplot2) order_var = 'displ' mpg %>% mutate(manufacturer = fct_reorder(manufacturer, order_var)) #> Error in mutate_impl(.data, dots): Evaluation error: length(f) == length(.x) is not TRUE. Tried with bang bang !! : mpg %>% mutate(manufacturer = fct_reorder(manufacturer, !!order_var)) #> Error in mutate_impl(.data, dots): Evaluation error: length(f) == length(.x) is not TRUE. Tried with