Panos Kalatzantonakis
Panos Kalatzantonakis

Reputation: 12683

Merge values of a factor column

Column data$form contains 170 unique different values, (numbers from 1 to ~800). I would like to merge some values (e.g with a 10 radius/step).

I need to do this in order to use:

colors = rainbow(length(unique(data$form)))

In a plot and provide a better visual result.

Thank you in advance for your help.

Upvotes: 2

Views: 131

Answers (2)

MrGumble
MrGumble

Reputation: 5776

You could also use cut. If you use the argument labels=FALSE, you get an integer value:

form <- runif(170, min=1,max=800)
> cut(form, breaks=20)
[1] (518,558]  (280,320]  (240,280]  (121,160]  (757,797]  
[6] (160,200]  (320,359]  (598,638]  (80.8,121] (359,399]  
[7] (121,160]  (200,240] ...
20 Levels: (1.18,41] (41,80.8] (80.8,121] (121,160] (160,200] (200,240] (240,280] (280,320] (320,359] (359,399] (399,439] ... (757,797]

> cut(form, breaks=20, labels=FALSE)
 [1] 14  8  7  4 20  5  9 16  3 10  4  6  5 18 18  6  2 12  
[19] 2  19 13 11 13 11 14 12 17  5 ...

On a side-note, I want you to re-consider plotting with rainbow colours, as it distorts reading the data, cf. Rainbow Color Map (Still) Considered Harmful.

Upvotes: 3

Ricardo Saporta
Ricardo Saporta

Reputation: 55420

you can use %/% to group them and mean to combine them and normalize to scale them.

 # if you want specifically 20 groups:
 groups <- sort(form) %/% (800/20) 

 x <- c(by(sort(form), groups, mean))

 x <- normalize(x, TRUE) * 19 + 1

        0         1         2         3         4 
 1.000000  1.971781  2.957476  4.103704  4.948560 
        5         6         7         8         9 
 5.950617  7.175309  7.996914  8.953086  9.952263 
       10        11        12        13        14 
10.800705 11.901235 12.888889 13.772291 14.888889 
       15        16        17        18        19 
15.927984 16.864198 17.918519 18.860082 20.000000 

Upvotes: 3

Related Questions