Hi, community
I met a little problem when practicing R. As we know, using cut function or else, we could divide the data into different groups. My question is how could we just generate interval character? For example min=0, max=29, interval=2, generate character series like [0,2)、[2,4)、.........[28,30), not input them one by one.
Thanks for all the help.

Hi @Comede_way! You could check out the `santoku` package.

``````library(santoku)

chop_evenly(0:30, intervals = 15) |>
levels()
#>  [1] "[0, 2)"   "[2, 4)"   "[4, 6)"   "[6, 8)"   "[8, 10)"  "[10, 12)"
#>  [7] "[12, 14)" "[14, 16)" "[16, 18)" "[18, 20)" "[20, 22)" "[22, 24)"
#> [13] "[24, 26)" "[26, 28)" "[28, 30]"
``````

Created on 2023-07-31 with reprex v2.0.2.9000

Thank you. @scottyd22
Is it possible to let [28,30)→[28,30)?

Hi @Comede_way. I'm afraid don't understand the question. What is meant by [28,30)→[28,30)?

@scottyd22 I am sorry for that. Is it possible [28,30]→[28,30).The 30 is just a boundary value as i thought. Maybe the true value is 29.9.

Got it. You can add the argument `close_end = F` to create the open bracket. However, in doing so, it looks like "{30}" becomes a standalone level. The example gives a way to remove it from the set.

``````library(santoku)

my_levels = chop_evenly(0:30, intervals = 15, close_end = F) |> levels()

# levels with the standalone
my_levels
#>  [1] "[0, 2)"   "[2, 4)"   "[4, 6)"   "[6, 8)"   "[8, 10)"  "[10, 12)"
#>  [7] "[12, 14)" "[14, 16)" "[16, 18)" "[18, 20)" "[20, 22)" "[22, 24)"
#> [13] "[24, 26)" "[26, 28)" "[28, 30)" "{30}"

# remove the standalone
my_levels[my_levels != '{30}']
#>  [1] "[0, 2)"   "[2, 4)"   "[4, 6)"   "[6, 8)"   "[8, 10)"  "[10, 12)"
#>  [7] "[12, 14)" "[14, 16)" "[16, 18)" "[18, 20)" "[20, 22)" "[22, 24)"
#> [13] "[24, 26)" "[26, 28)" "[28, 30)"
``````
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.