MCS
MCS

Reputation: 1101

Grouping numeric values into categories in R?

I have a variable indicating industrial sectors taking values in between 1-100; at irregular intervals these sectors can be grouped in 20 macro-sectors (e.g. 1-5 -> sector_1, 6-12 -> sector_2...).

What is the most efficient way to transform the first variable in the second?

I am considering a function such as the following but the solution is not really efficient and a little ugly:

index <- function(x) {
  if (x<= 5){
    x <- "Sector_1"
  }
  if (x>5 & x<=12){
    x <- "Sector_3"
  }
return(x)
}

Upvotes: 2

Views: 3793

Answers (1)

Andre Silva
Andre Silva

Reputation: 4928

As suggested by @r2evans, use cut. Here is a reproducible example in the context of your question:

set.seed(1) #make results reproducible. 
sector <- data.frame(mini.sector = seq(1,10,1), value = round(runif(10, 1, 100), 0))

#name macro sectors as 'a', 'b', 'c' and 'n' and assign them to micro sectors based on defined value cuts.
sector$macro.sector <- cut(sector$value, c(-Inf, 10, 25, 50, Inf), labels=c("a", "b", "c", "n"))

head(sector) #show first five row from data frame 'sector'.

mini.sector  value  macro.sector
          1     27             c
          2     38             c
          3     58             n
          4     91             n
          5     21             b
          6     90             n

Upvotes: 1

Related Questions