Search code examples
rrescale

I don't know how I can rescale my data? (e.g. 0~50 :1 50~100: 2 and so on..)


I want to rescale my data.
for example:

if (0 <= data[i] <= 50) data[i] = 1 
if (50 < data[i] <= 100) data[i] = 2
if (100 < data[i] <= 150) data[i] = 3 

etc ...

I can do it tediously by using "for" and "if" but the maximum value is more than 3000.
Any good ideas?


Solution

  • The cut command is useful for splitting up regions, especially those with irregular size.

    #test data
    x<-c(1,25,50,75,100,122,150, 770)
    
    #cut
    nx<-cut(x, breaks=c(0,50,100,150,Inf), include.lowest=T, labels=F)
    
    #compare
    cbind(n, nx)
    
    #        x  
    # [1,]   1 1
    # [2,]  25 1
    # [3,]  50 1
    # [4,]  75 2
    # [5,] 100 2
    # [6,] 122 3
    # [7,] 150 3
    # [8,] 770 4
    

    Or if you really all your divisions are of 50, then as @Lashane pointed out

    ceiling(x/50)
    

    will do them all without the if as well without having to specify all the breaks.