How to get the pairwise difference of all values within uneven categories in R

I found solutions for simple vectors, but is there a way to make all pairwise differences using dplyr or base R for all the elements in a category?

library(tidyverse)
x = 1:10
y = rep(letters[1:5],each=2)
z = rep(1:2,length.out =10)
df = data.frame(x,y, z)
df = rbind(df,c(11,"e",3))
df$verif = paste0(df$y,df$z)
df$x = as.numeric(df$x)
df %>% 
  group_by(y) %>% 
  summarise(Diff = abs(x - lag(x)))

gives:

`summarise()` regrouping output by 'y' (override with `.groups` argument)
# A tibble: 11 x 2
# Groups:   y [5]
   y      Diff
   <chr> <dbl>
 1 a        NA
 2 a         1
 3 b        NA
 4 b         1
 5 c        NA
 6 c         1
 7 d        NA
 8 d         1
 9 e        NA
10 e         1
11 e         1

In this example, it's only using the previous value in the data frame, therefore missing pairwise differences (look at 9, 10 and 11 for group "e" ).

Is there a way to get all the pairwise differences in each category? Keeping track of the pairwise differences would be useful as well (e.g., e1 with e2 = 1, e2 with e3 is = 1 and e1 with e3 is =2)

I tired the outer() function but wasn't able to make it work as well as the dist() function.

I continued to try and found this:

my.df=df %>% 
  group_by(y) %>% 
  summarise(Diff = combn(x,2,diff))

my.df
# A tibble: 7 x 2
# Groups:   y [5]
  y      Diff
  <chr> <dbl>
1 a         1
2 b         1
3 c         1
4 d         1
5 e         1
6 e         2
7 e         1

I just now need to get which pairwise difference was calculated...

Continued again and got this mess:

my.df=df %>% 
  group_by(y) %>% 
  summarise(Diff = combn(x,2,diff),
            test = combn(verif,2,paste, simplify = FALSE)) %>%  
  mutate(test2 = paste0(test, collapse = "-"))
my.df
> my.df
# A tibble: 7 x 4
# Groups:   y [5]
y      Diff test      test2                                                  
<chr> <dbl> <list>    <chr>                                                  
  1 a         1 <chr [2]> "c(\"a1\", \"a2\")"                                    
2 b         1 <chr [2]> "c(\"b1\", \"b2\")"                                    
3 c         1 <chr [2]> "c(\"c1\", \"c2\")"                                    
4 d         1 <chr [2]> "c(\"d1\", \"d2\")"                                    
5 e         1 <chr [2]> "c(\"e1\", \"e2\")-c(\"e1\", \"e3\")-c(\"e2\", \"e3\")"
6 e         2 <chr [2]> "c(\"e1\", \"e2\")-c(\"e1\", \"e3\")-c(\"e2\", \"e3\")"
7 e         1 <chr [2]> "c(\"e1\", \"e2\")-c(\"e1\", \"e3\")-c(\"e2\", \"e3\")"


from Recent Questions - Stack Overflow https://ift.tt/34mBm4c
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)