set.seed(123)
my_data = data.frame(var1 = rnorm(100,100,100))
min = min(my_data$var1)
max = max(my_data$var)
Here is what I am trying to do:
Starting from the smallest value of var1, I would like to create a variable that groups values of var1 by some "fixed increment" (e.g. by 10) until the maximum value of var1 is reached
Then, I would then like to create another variable which labels each of these groups by the min/max value of that group
Here is my attempt to do this:
# create a vector of increments
breaks <- seq(min(my_data$var1), max(my_data$var1), by = 10)
# initialize new variables
my_data$class <- NA
my_data$label <- NA
# get the number of breaks
n <- length(breaks)
# Loop
for (i in 1:(n - 1)) {
# find which "class" (i.e. break) each value of var1 is located within
indices <- which(my_data$var1 > breaks[i] & my_data$var1 <= breaks[i + 1])
# make assignment
my_data$class[indices] <- i
# create labels
my_data$label[indices] <- paste(breaks[i], breaks[i + 1])
}
The code seems to have run, but I am not sure if this is correct (I don't think I have done this correctly because I see some NA's).
Can someone please tell show me how to do this correctly?
pretty just produces a vector of numbers: pretty function - RDocumentation - so you can change 10 to something else -- it is the lower end used in cut()