Hi there, does anyone know why base R's cut() function formats output bins in the (] convention, as opposed to, say, () or []. Is there a way to change how the output is formatted?
@Leon is correct! If you have a value that lands right on the cut (eg. precisely 10, in your example), you have to determine which bin it falls into. In your example, a value of 10 would fall into (0, 10], not (10, 20].
FWIW, you can use the right argument in cut to determine whether intervals are closed (ie. they include values) on the right (highest) or on the left. I don't think you can substitute the characters used in the notation without doing some further string processing, though. Converting the cut factor labels to strings and then using stringr functions inside mutate is probably the way to go.
EDIT: I should mention that you can also pass a labels argument to cut if you'd like to skip the automatically produced labels. You do lose the advantage of having nicely formatted automatic labels, though, so that may be more or less attractive an option depending on your needs.
There is likely a more concise way to write the regex in the gsub, but as a regex novice I find that pattern intuitive (i.e., separately escape \\ each bracket type and combine with |).
Possibly more efficient than changing the labels after the fact is writing a small utility function to create labels in whatever format you prefer. For example: