gm007
gm007

Reputation: 587

Factor data frame values into quartile/decile ranges

I'm trying to create decile factors corresponding to my dataframe's values. I would like the factors to appear as a range e.g. if the value is "164" then the factored result should be "160 - 166".

In the past I would do this:

quantile(countries.Imported$Imported, seq(0,1, 0.1), na.rm = T) # display deciles
Imported.levels <- c(0, 1000, 10000, 20000, 30000, 50000, 80000) # create levels from observed deciles
Imported.labels <- c('< 1,000t', '1,000t - 10,000t', '10,000t - 20,000t', etc) # create corresponding labels 
colfunc <- colorRampPalette(c('#E5E4E2', '#8290af','#512888'))

# apply factor function
Imported.colors <- colfunc(10)
names(Imported.colors) <- Imported.labels
countries.Imported$Imported.fc <- factor(
cut(countries.Imported$Imported, Imported.levels),labels = Imported.labels)

Instead, I would like to apply a function that will factor the values into decile range. I want to avoid manually setting factor labels since I will be running many queries and plotting maps that have discrete legends. I've created a column called Value.fc but I cannot format it to "160 - 166" from "(160, 166]". Please see the problematic code below:

corn_df <- corn_df %>%
mutate(Value.fc = gtools::quantcut(Value, 10))

corn_df %>%
select(Value, unit_desc, domain_desc, Value.fc) %>%
head(6)
A tibble: 6 x 4
Value     unit_desc     domain_desc  Value.fc 
<dbl>     <chr>         <chr>        <fct>    
1  164.   BU / ACRE     TOTAL        (160,166]
2  196.   BU / ACRE     TOTAL        (191,200]
3  203.   BU / ACRE     TOTAL        (200,230]
4  205.   BU / ACRE     TOTAL        (200,230]
5  172.   BU / ACRE     TOTAL        (171,178]
6  213.   BU / ACRE     TOTAL        (200,230]

Upvotes: 2

Views: 939

Answers (1)

DJV
DJV

Reputation: 4863

You can try to use dplyr::ntile() or Hmisc::cut2().

If you're interested where the decline of the variable starts and ends you can use Hmisc::cut2() and stringr::str_extract_all()

require(tidyverse)
require(Hmisc)
require(stringr)

df <- data.frame(value = 1:100) %>%
  mutate(decline = cut2(value, g=10),
         decline = factor(sapply(str_extract_all(decline, "\\d+"),
                          function(x) paste(x, collapse="-"))))

head(df) 

  value decline
1     1    1-11
2     2    1-11
3     3    1-11
4     4    1-11
5     5    1-11
6     6    1-11

If you're looking only for the decline of the variable you can use dplyr::ntile().

require(tidyverse)

df <- data.frame(value = 1:100) %>%
  mutate(decline = ntile(value, 10))  

head(df)

  value decline
1     1       1
2     2       1
3     3       1
4     4       1
5     5       1
6     6       1

Upvotes: 1

Related Questions