I have a data set, and I would like to apply an equation to groups of my values. Specifically I would like to apply
sqrt(X^2+Y^2+Z^2)
to all values within a specific time and variable
Looking at the data below I would like to group my values by unique time (TS) and Bins (Bin), and grab the square root of the sum of squares for each of the X Y and Z components.
id D Bin value Month Day Year Hour Minute Second TS
1 X V1 -0.320 1 30 2012 13 59 50 2012-01-30 13:59:50
1 Y V1 -0.088 1 30 2012 13 59 50 2012-01-30 13:59:50
1 Z V1 0.171 1 30 2012 13 59 50 2012-01-30 13:59:50
1 X V2 0.368 1 30 2012 13 59 50 2012-01-30 13:59:50
1 Y V2 -0.104 1 30 2012 13 59 50 2012-01-30 13:59:50
1 Z V2 0.008 1 30 2012 13 59 50 2012-01-30 13:59:50
2 X V1 -0.052 1 30 2012 14 0 50 2012-01-30 14:00:50
2 Y V1 0.278 1 30 2012 14 0 50 2012-01-30 14:00:50
2 Z V1 -0.086 1 30 2012 14 0 50 2012-01-30 14:00:50
2 X V2 -0.214 1 30 2012 14 0 50 2012-01-30 14:00:50
2 Y V2 0.118 1 30 2012 14 0 50 2012-01-30 14:00:50
2 Z V2 -0.030 1 30 2012 14 0
So up first would be V1 at 13:59:50
sqrt(-0.320^2 + -0.088^2 + 0.171^2)
and then for V2 at t13:59:50
sqrt(0.368^2 +-0.104^2 + 0,008^2)
and so on
I had tried to use this formula (Data is called “V”)
V=aggregate(value~TS+variable,data=V,sqrt((if(V$D=="X")V$value^2)+(if(V$D=="Y")V$value^2))+(if(V$D=="Z")V$value^2))
But obviously that does not work. So does anyone have a better way to first index unique groups in a data set, and than apply an equation to said group?
Assuming you always have one X, one Y, and one Z for each combination of (TS, Bin), I would try this: