Reputation: 853
I have two sets of values as follows:
x <- as.vector(c(1, 2, 3, 2, 1))
y <- as.vector(c(7, 7, 8, 9, 9))
I am trying to calculate the mean difference of the values in each group and also the mean difference of the values between groups.
In this small example, the mean differences would be, for x, (1+2+1+0+1+0+1+1+2+1)/10=10/10=1
And for y it would be (0+1+2+2+1+2+2+1+1+0)/10=12/10=1.2
Between the groups, it would be (6+6+7+8+8+5+5+6+7+7+4+4+5+6+6+5+5+6+7+7+6+6+7+8+8)/25=155/25=6.2
I am hoping that there is some way to do this with code that is simpler.
Upvotes: 1
Views: 95
Reputation: 39858
One possibility could be:
xy_diff <- abs(sapply(x, "-", y))
sum(xy_diff)/(dim(xy_diff)[1]*dim(xy_diff)[2])
[1] 6.2
x_diff <- abs(sapply(x, "-", x))
x_diff <- x_diff[upper.tri(x_diff)]
sum(x_diff/length(x_diff))
[1] 1
y_diff <- abs(sapply(y, "-", y))
y_diff <- y_diff[upper.tri(y_diff)]
sum(y_diff/length(y_diff))
[1] 1.2
Upvotes: 1