Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Created May 9, 2018 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-dray/7d8527d9fb54b5c875984aa1679e1f87 to your computer and use it in GitHub Desktop.
Save matt-dray/7d8527d9fb54b5c875984aa1679e1f87 to your computer and use it in GitHub Desktop.
Using the binwidths of a histogram object to bin values in the dataframe
# 1. Fake dataset
df <- data.frame(id = 1:1000, value = sample(10000:50000, 1000))
# 2. Histogram object for accessing binwidths
hist_df <- hist(
df$value, # column of data to be binned
(50000-10000)/500 # bins of width 500 from 10k to 50k
)
# 3. Bin the data
df1 <- transform(
df, # your dataframe
group = cut(
value, # column with data to bin
breaks = hist_df$breaks, # the breakpoints from the histogram
labels = paste( # create a label by pasting binwidth values
hist_df$breaks[1:length(hist_df$breaks)-1],
"to",
hist_df$breaks[2:length(hist_df$breaks)]
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment