data(airquality)
month_num = airquality$Month
table(month_num)
# Grouping the first two values
table(bin(month_num, 5:6))
# ... plus changing the name to '10'
table(bin(month_num, list("10" = 5:6)))
# ... and grouping 7 to 9
table(bin(month_num, list("g1" = 5:6, "g2" = 7:9)))
# Grouping every two months
table(bin(month_num, "bin::2"))
# ... every 2 consecutive elements
table(bin(month_num, "!bin::2"))
# ... idem starting from the last one
table(bin(month_num, "!!bin::2"))
# Using .() for list():
table(bin(month_num, .("g1" = 5:6)))
#
# with non numeric data
#
month_lab = c("may", "june", "july", "august", "september")
month_fact = factor(month_num, labels = month_lab)
# Grouping the first two elements
table(bin(month_fact, c("may", "jun")))
# ... using regex
table(bin(month_fact, "@may|jun"))
# ...changing the name
table(bin(month_fact, list("spring" = "@may|jun")))
# Grouping every 2 consecutive months
table(bin(month_fact, "!bin::2"))
# ...idem but starting from the last
table(bin(month_fact, "!!bin::2"))
# Relocating the months using "@d" in the name
table(bin(month_fact, .("@5" = "may", "@1 summer" = "@aug|jul")))
# Putting "@" as first item means subsequent items will be placed first
table(bin(month_fact, .("@", "aug", "july")))
#
# "Cutting" numeric data
#
data(iris)
plen = iris$Petal.Length
# 3 parts of (roughly) equal size
table(bin(plen, "cut::3"))
# Three custom bins
table(bin(plen, "cut::2]5]"))
# .. same, excluding 5 in the 2nd bin
table(bin(plen, "cut::2]5["))
# Using quartiles
table(bin(plen, "cut::q1]q2]q3]"))
# Using percentiles
table(bin(plen, "cut::p20]p50]p70]p90]"))
# Mixing all
table(bin(plen, "cut::2[q2]p90]"))
# NOTA:
# -> the labels always contain the min/max values in each bin
# Custom labels can be provided, just give them in the char. vector
# NA values lead to the default label
table(bin(plen, c("cut::2[q2]p90]", "<2", "]2; Q2]", NA, ">90%")))
#
# With a formula
#
data(iris)
plen = iris$Petal.Length
# We need to use "x"
table(bin(plen, list("< 2" = ~x < 2, ">= 2" = ~x >= 2)))
Run the code above in your browser using DataLab