Hello,
Say I have a simple data frame like this (in reality my data frame is larger).
Group = c("d","d","d","t","t","t")
ID = c("d1","d2","d3","t1","t2","t3")
Value = c(15, 10,12,25,20, 22)
df<-data.frame(Group,ID,Value)
I want to find a way to calculated the absolute differences between all combinations of ID values ( excluding itself) within the same “Group”. These values should then be combined (in the column NewValues) into a new data frame that looks like this:
Group = c("d","d","d","t","t","t")
Diff= c("d1d2","d1d3","d2d3","t1t2","t1t3","t2t3")
NewValue=c(5,3,2,3,5,8)
dfNew<-data.frame(Group,Diff,NewValue)
Also, if that calculation cannot be done (i.e. group 5 is missing), NA values for the 'NewValue' column would be ideal.
I tried to use the group_by and mutate function of dplyr but without much success. Help would be very much appreciated!
Mike