Dataframe R中组内所有行之间的数值差

Dataframe R中组内所有行之间的数值差,dataframe,r,data-analysis,Dataframe,R,Data Analysis,我有一个看起来有点像 Indices<-data.frame("Animal"=c("Cat", "Cat", "Cat", "Dog", "Dog", "Dog", "Dog", "Bird", "Bird"), "Trend"=c(1,3,5,-3,1,2,4,2,1), "Project"=c("ABC", "ABC2", "EDF", "ABC", "EDF", "GHI", "ABC2", "ABC

我有一个看起来有点像

Indices<-data.frame("Animal"=c("Cat", "Cat", "Cat", "Dog", "Dog", "Dog", "Dog", "Bird", 
                    "Bird"), "Trend"=c(1,3,5,-3,1,2,4,2,1), "Project"=c("ABC", "ABC2",
                    "EDF", "ABC", "EDF", "GHI", "ABC2", "ABC", "GHI"))
但这只显示了紧随其后的行之间的差异,我正在尝试查看组中所有行之间的差异。它还提供了差异,但没有告诉我值是否为
=3

我更希望最终结果是一个动物和项目名称的列表,它们的绝对趋势差异
>=3

Animal     TrendDiff     Projects
Cat            4          ABC-EDF
Dog            7          ABC-ABC2
Dog            3          ABC2-EDF
Dog            4          ABC-EDF
Dog            5          ABC-GHI

我有超过200个不同的“动物”组和超过400行,所以需要它是不需要指定每一行的东西。我还是个新手,所以请具体回答。谢谢

一种方法是将
索引
data.frame与自身连接起来

library(dplyr)
Indices %>% 
      left_join(Indices, by = "Animal") %>%
      filter(Project.x != Project.y) %>%
      mutate(TrendDiff = Trend.x - Trend.y) %>% 
      filter(TrendDiff >= 3) 

# A tibble: 5 x 6
# Groups:   Animal [2]
# Animal  Trend.x  Project.x Trend.y Project.y  TrendDiff
# cat          5   EDF             1 ABC               4
# Dog          1   EDF            -3 ABC               4
# Dog          2   GHI            -3 ABC               5
# Dog          4   ABC2           -3 ABC               7
# Dog          4   ABC2            1 EDF               3
library(dplyr)
Indices %>% 
      left_join(Indices, by = "Animal") %>%
      filter(Project.x != Project.y) %>%
      mutate(TrendDiff = Trend.x - Trend.y) %>% 
      filter(TrendDiff >= 3) 

# A tibble: 5 x 6
# Groups:   Animal [2]
# Animal  Trend.x  Project.x Trend.y Project.y  TrendDiff
# cat          5   EDF             1 ABC               4
# Dog          1   EDF            -3 ABC               4
# Dog          2   GHI            -3 ABC               5
# Dog          4   ABC2           -3 ABC               7
# Dog          4   ABC2            1 EDF               3