繁体   English   中英

如何获得 R 中不均匀类别内所有值的成对差异

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

我找到了简单向量的解决方案,但是有没有办法使用 dplyr 或基本 R 对类别中的所有元素进行所有成对差异?

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)))

给出:

`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

在此示例中,它仅使用数据框中的前一个值,因此缺少成对差异(查看组 "e" 的 9、10 和 11)。

有没有办法获得每个类别中的所有成对差异? 跟踪成对差异也很有用(例如,e1 与 e2 = 1,e2 与 e3 为 = 1,e1 与 e3 为 =2)

我厌倦了outer() function,但无法让它像dist() function一样工作。

我继续尝试发现:

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

我现在需要得到计算出成对差异......

再次继续并得到这个烂摊子:

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\")"

知道了:

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)

my.df=df %>% 
  group_by(y) %>% 
  summarise(Diff = combn(x,2,diff),
            test = combn(verif,2,paste, simplify = FALSE)) %>%  
  mutate(test2 = unlist(lapply(test, function(x)paste(x,collapse="-")))) %>%  
  select(-test)

这是 output

my.df
# A tibble: 7 x 3
# Groups:   y [5]
  y      Diff test2
<chr> <dbl> <chr>
1 a         1 a1-a2
2 b         1 b1-b2
3 c         1 c1-c2
4 d         1 d1-d2
5 e         1 e1-e2
6 e         2 e1-e3
7 e         1 e2-e3

你可以这样做:

library(tidyverse)

df %>%
  group_by(y) %>%
  summarise(result = combn(seq_along(x), 2, function(i)
                     list(test1 = diff(x[i]),  #The difference
                          test2 = paste0(verif[i], collapse = '-')), # The pairs
                     simplify = FALSE),
             .groups = 'drop') %>%
  unnest_wider(result)

# A tibble: 7 x 3
  y     test1 test2
  <chr> <dbl> <chr>
1 a         1 a1-a2
2 b         1 b1-b2
3 c         1 c1-c2
4 d         1 d1-d2
5 e         1 e1-e2
6 e         2 e1-e3
7 e         1 e2-e3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM