How to group vector values

Hello, I extracted a column of values from my dataframe and sorted them. I am now left with the values of that column and I have assigned a variable to these values. How do I group these values from e.g group 1: <=90 (so valued less than or equal to 90)
Group 2: 90 <=180 and so forth until I reach 360??

Would really appreciate the help.

1 Like

Hi, is it below what you want ?

library(data.table)
library(tidyverse)

my_data <-  c(1:360) %>% data.frame()

colnames(my_data)[1] <-  "numbers"

my_data <- setDT(my_data)


my_data[,Categorized_numbers := cut(numbers,
                        breaks=c(0, 90, 180, 270, 360),
                        include.lowest=TRUE,
                        labels=c("<90", "<180", "<270", "<360"))]

my_data[,table(Categorized_numbers)]

X1 <- split(my_data, my_data$Categorized_numbers) %>% as.data.frame() %>% select(.,!contains("Categorized"))

is it possible to do this without tidy verse?

I just want to take the column and divide it into values from <= 90, 90 <= 180 and so on. I can do it for the first one but then the rest I get an error as it does not recognise the 90 <= 180

You didn't show your data, so I created an example data and my code produces the following tables (dataframes):

obraz

obraz

see data (see below) this is an extracted column from a large Dataset called shalan1999,
this is what I am trying to achieve:

1 : {0 <= 90}

2 : {90 <= 180}

3 : {180 <= 270}

4 : {270 <= 360}

[1] 0 0 0 0 0 0 0 3 3 7 10 10 10 10 10 11 15 17 20 20 20 20 20
[24] 20 20 21 25 25 25 30 30 30 33 33 43 48 60 65 70 78 80 80 80 80 80 87
[47] 90 90 95 100 100 118 127 130 130 130 140 140 145 145 150 150 155 160 160 170 170 173 175
[70] 175 175 175 180 180 180 180 185 185 190 190 190 190 190 190 195 200 200 200 210 210 210 210
[93] 210 215 215 216 220 225 230 230 235 244 245 245 250 250 260 265 280 285 290 290 295 295 300
[116] 305 310 310 310 312 313 313 315 315 318 318 320 320 328 330 330 330 330 335 340 340 340 345
[139] 345 345 350 350 355 355 355 355

is there not a much simpler way to do it as I am only a beginner and have little experience?

Is there a reason you cannot use the tidyverse? Using case_when() from the dplyr package makes it quite simple.

I am doing this for University and our instructor asked us to do it without in order to grasp the basics.

Then nested ifelse() functions should work just fine too.

Please familiarise yourself with the following:

Also, please use meaningful thread titles instead of "Need help" or something like that. That will significanty improve chance of answers from the menbers.


Now that moderation stuff is done, I'll share the following pointers, without providing any direct solution.

  1. You absolutely do not need tidyverse or data.table.
  2. There is no reason to use case_when of ifelse or if_else here, as there is no need to specify all intervals manually. In this particular case, cut function, suggested by Andrzej already, is perfect, and it comes from base R only.
  3. cut can be applied on vectors directly, no need to convert to data.frame or related classes.

Hope this helps.

1 Like

This topic was automatically closed 21 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.