문제

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.

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top