Created
May 9, 2018 09:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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