Gopi Gopi - 2 months ago 6
R Question

Data frame transformation in R, normalization according to parameters

I am not sure the title is clear, sorry.

I have a data frame:


mesh procs ordering L1 L2 L3 Time
1 carabiner 1 ori 0.430610 0.49491 0.430610 6.154280
4 carabiner 2 ori 0.232120 0.63542 0.232120 3.168580
7 carabiner 4 ori 0.106630 0.61051 0.106630 0.549437
10 carabiner 8 ori 0.077175 0.64292 0.077175 0.284768
13 carabiner 16 ori 0.065619 0.65944 0.065619 0.161521
16 carabiner 24 ori 0.067526 0.70776 0.067526 0.113796
19 carabiner 32 ori 0.077073 0.68884 0.077073 0.109517
22 crake 1 ori 0.441070 0.47276 0.441070 5.621140
25 crake 2 ori 0.181260 0.51812 0.181260 2.219040
28 crake 4 ori 0.105680 0.45235 0.105680 0.507697
31 crake 8 ori 0.062938 0.71500 0.062938 0.279446
34 crake 16 ori 0.057988 0.84940 0.057988 0.169796
37 crake 24 ori 0.070777 0.57910 0.070777 0.124948
40 crake 32 ori 0.082224 0.71482 0.082224 0.107725
43 dialog 1 ori 0.561330 0.37291 0.561330 5.599430
46 dialog 2 ori 0.218940 0.49722 0.218940 2.432410
49 dialog 4 ori 0.126080 0.54000 0.126080 0.522720
52 dialog 8 ori 0.072740 0.66879 0.072740 0.320339
55 dialog 16 ori 0.070624 0.74442 0.070624 0.174091
58 dialog 24 ori 0.059578 0.70210 0.059578 0.132421
61 dialog 32 ori 0.080391 0.62270 0.080391 0.111252


I would like to add a column called
speedup
, where for line x, the column gets Tx/Time[x] where Tx is the
Time
value for the line that has the same mesh value than x and 1 in procs.

Answer

I would do

library(data.table)
setDT(DF)[ , speedup := Time[procs==1]/Time, by=mesh]

which leads to

         mesh procs ordering       L1      L2       L3     Time   speedup
 1: carabiner     1      ori 0.430610 0.49491 0.430610 6.154280  1.000000
 2: carabiner     2      ori 0.232120 0.63542 0.232120 3.168580  1.942283
 3: carabiner     4      ori 0.106630 0.61051 0.106630 0.549437 11.201066
 4: carabiner     8      ori 0.077175 0.64292 0.077175 0.284768 21.611557
 5: carabiner    16      ori 0.065619 0.65944 0.065619 0.161521 38.102042
 6: carabiner    24      ori 0.067526 0.70776 0.067526 0.113796 54.081690
 7: carabiner    32      ori 0.077073 0.68884 0.077073 0.109517 56.194746
 8:     crake     1      ori 0.441070 0.47276 0.441070 5.621140  1.000000
 9:     crake     2      ori 0.181260 0.51812 0.181260 2.219040  2.533140
10:     crake     4      ori 0.105680 0.45235 0.105680 0.507697 11.071840
11:     crake     8      ori 0.062938 0.71500 0.062938 0.279446 20.115300
12:     crake    16      ori 0.057988 0.84940 0.057988 0.169796 33.105256
13:     crake    24      ori 0.070777 0.57910 0.070777 0.124948 44.987835
14:     crake    32      ori 0.082224 0.71482 0.082224 0.107725 52.180460
15:    dialog     1      ori 0.561330 0.37291 0.561330 5.599430  1.000000
16:    dialog     2      ori 0.218940 0.49722 0.218940 2.432410  2.302009
17:    dialog     4      ori 0.126080 0.54000 0.126080 0.522720 10.712102
18:    dialog     8      ori 0.072740 0.66879 0.072740 0.320339 17.479701
19:    dialog    16      ori 0.070624 0.74442 0.070624 0.174091 32.163811
20:    dialog    24      ori 0.059578 0.70210 0.059578 0.132421 42.285061
21:    dialog    32      ori 0.080391 0.62270 0.080391 0.111252 50.331050
         mesh procs ordering       L1      L2       L3     Time   speedup

Assuming procs==1 is always the first row within a mesh group, this also works:

DF$speedup <- with(DF, ave(Time, mesh, FUN = function(x) x[1]/x))
Comments