I am trying to get pairwise differences for price between localities. My data look like this:
table=NULL
table$id= 1:9
table$locality= c("A", "B", "C")
table$price= rnorm(9, 444, 322)
table$concat=paste(table$id, table$locality)
final=data.frame(table)
final
id locality price concat
1 A 379.1501 1 A
2 B 792.3608 2 B
3 C 762.0627 3 C
4 A 439.0378 4 A
5 B 100.2860 5 B
6 C 830.2024 6 C
7 A 148.5925 7 A
8 B 668.3819 8 B
9 C 510.3919 9 C
My goal is get if its possible get a table like this variable, in my below table concat is id:
diff_A-B | diff_A-C | diff_B-C
1A-2B | 1A-3C | 2B-3C
1A-5B | 1A-6C | 2B-6C
1A-8B | 1A-9C | 2B-9C
4A-2B | 4A-3C | 5B-3C
4A-5B | 4A-6C | 5B-6C
4A-8B | 4A-9C | 5B-9C
7A-2B | 7A-3C | 8B-3C
7A-5B | 7A-6C | 8B-6C
7A-8B | 7A-9C | 8B-9C
I tried:
library(dplyr)
table %>%
arrange(id, locality) %>%
group_by(concat) %>%
mutate(variables=outer(price,price, "-"))
But the output doesn't show me the results that I need.
Please any advice will be appreciated.