问题
I've got this csv table for which I need to rescale data between 0 and 1 per each column. That is, the lowest value of any given column will be 0, the highest will be 1, and all other values will be linearly scaled accordingly. Here's my script:
tableau <- read.csv("/tableau.csv")
tableau.m <- melt(tableau)
tableau.m <- ddply(tableau.m, .(variable), transform,rescale = rescale(value))
(And here's the data: https://dl.dropboxusercontent.com/u/73950/tableau.csv)
The issue is that I need the second column ("B") to be inverted. That is, for this column only and not for the others, the lowest value should be 1, and the highest should be 0.
Is plyr flexible that way, or should I try other ways to achieve this?

(In this example, column B should read with 2.13 being white, 1.88 being dark blue, and 2.07's, 2.09's, 2.05's shades being scaled accordingly. The other column should be left untouched.)
回答1:
What about using ifelse
to select the scaling direction, based on the value of variable
:
tableau.m = ddply(tableau.m, .(variable), transform,
rescale = ifelse(variable=="B",
rescale(value, to=c(1,0)), rescale(value)))
Net variable value rescale
1 a B 1.88 1.00000000
2 b B 2.05 0.32000000
3 c B 2.09 0.16000000
4 d B 2.07 0.24000000
5 e B 2.13 0.00000000
6 a C 0.15 0.00000000
7 b C 0.23 0.21621622
8 c C 0.29 0.37837838
9 d C 0.52 1.00000000
10 e C 0.30 0.40540541
11 a D 0.60 1.00000000
12 b D 0.51 0.72727273
13 c D 0.40 0.39393939
14 d D 0.36 0.27272727
15 e D 0.27 0.00000000
16 a E..e. 10.00 0.04109589
17 b E..e. 55.00 0.65753425
18 c E..e. 58.00 0.69863014
19 d E..e. 80.00 1.00000000
20 e E..e. 7.00 0.00000000
21 a F..f. 90.00 1.00000000
22 b F..f. 80.00 0.00000000
23 c F..f. 88.00 0.80000000
24 d F..f. 84.00 0.40000000
25 e F..f. 90.00 1.00000000
来源:https://stackoverflow.com/questions/30078074/rescaling-with-plyr-ddply-in-r