Rescaling with plyr (ddply) in R

I have this csv table for which I need to rescale data between 0 and 1 for each column. That is, the smallest value of any given column will be 0, the highest will be 1, and all other values ​​will be linearly scaled accordingly. Here is my script:

tableau  <- read.csv("/tableau.csv")
tableau.m <- melt(tableau)
tableau.m <- ddply(tableau.m, .(variable), transform,rescale = rescale(value))

(And here is the data: https://dl.dropboxusercontent.com/u/73950/tableau.csv )

The problem is that I need to flip the second column ("B"). That is, for this column, and not for the rest, the lowest value should be 1, and the highest should be 0.

Is plyr flexible, or should I try other ways to achieve this?

enter image description here

( B 2.13, , 1,88 - , 2.07, 2.09 2.05 . .)

+1
1

ifelse 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
+1

All Articles