I am trying to create the Distance between the first observation and all other observations within a group. The first observation within group A is a1 and group B is b1.
I want a new column in df,"Euclidean', that would have the distance of each observation from the first observation calculated by group.
df <- data.table(Section = rep(c('A', 'B'), each = 4),
ID = c('a1','a2','a3','a4','b1','b2','b3','b4'),
x = c(5,10,15,15,10,15,30,25),
y = c(12,10,8,4,6,8,16,24))
Where distance calculation would be euclidean[a1,a2] = sqrt((x1-x2)^2+(y1-y2)^2). The first value in each group would be 0. I am hoping to accomplish this using dplyr or data.table. Thanks so much.