列data$form
包含170個唯一的不同值(從1到〜800的數字)。 我想合併一些值(例如,以10半徑/步長)。因子列的合併值
我需要這樣做才能使用:
colors = rainbow(length(unique(data$form)))
在情節和提供更好的視覺效果。
非常感謝您的幫助。
列data$form
包含170個唯一的不同值(從1到〜800的數字)。 我想合併一些值(例如,以10半徑/步長)。因子列的合併值
我需要這樣做才能使用:
colors = rainbow(length(unique(data$form)))
在情節和提供更好的視覺效果。
非常感謝您的幫助。
您可以使用%/%
對它們進行分組,並使用mean
將它們合併,並將它們與normalize
合併以對它們進行縮放。
# 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.9.888889 13.772291 14.888889
15 16 17 18 19
15.927984 16.864198 17.918519 18.860082 20.000000
您也可以使用cut
。如果您使用參數labels=FALSE
,你會得到一個整數值:
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 ...
在一個側面說明,我希望你能重新考慮與彩虹的顏色繪製,因爲它扭曲了讀取數據,參見Rainbow Color Map (Still) Considered Harmful。
當你說合並你的意思是將某些元素合併成一個單一的值?你如何確定你的端點或中心? – 2013-05-14 10:26:51
@RicardoSaporta,是裏卡多,我想合併這些值,因爲1和10之間沒有差別。它像一個學生學位,範圍從0到800,但是我想將它縮放到1到20。 – 2013-05-14 10:29:46