Domanda

So, I have an array of value from 1 to 100, and I need to make it discrete while applying an alias to each discrete value. For example:

  A
 10
 15
 55
 15
 70

Now, let's say I want to make it discrete over 2 bins (so that 0-50 is one bin and 51-100 is the other one) and alias these bins with 1 and 2. It should result in:

A
1
1
2
1
2

Please, notice this is different from the discretize function (contained in entropy or infotheo). That function only counts the number of values for each bin.

My question also is different from this one (with a similar title).

Now, I can have this result using a series of ifs, but I was wondering whether exist a simpler way to do it.

È stato utile?

Soluzione

You're looking for the function cut:

x <- cut(sample(1:100, 10), c(0, 50, 100))
str(x)
# Factor w/ 2 levels "(0,50]","(50,100]": 1 2 1 2 1 1 2 1 1 1
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top