是否有一个函数可以获取相同类型行之间的差异?

Is there a function that will allow to get the difference between rows of the same type?

我想找出相同类型的值的差异。

请参考下面的示例数据框:

df <- data.frame(
x = c("Jimmy Page","Jimmy Page","Jimmy Page","Jimmy Page", "John Smith", "John Smith", "John Smith",  "Joe Root", "Joe Root", "Joe Root", "Joe Root", "Joe Root"), 
y = c(1,2,3,4,5,7,89,12,34,67,95,9674 )
)

我想获得每个值的差异,例如吉米佩奇 = 1 和吉米佩奇 = 2,差异 = 1。

并呈现 NA 以区分不同的名称。

library(tidyverse)
df <-
  data.frame(
    x = c(
      "Jimmy Page",
      "Jimmy Page",
      "Jimmy Page",
      "Jimmy Page",
      "John Smith",
      "John Smith",
      "John Smith",
      "Joe Root",
      "Joe Root",
      "Joe Root",
      "Joe Root",
      "Joe Root"
    ),
    y = c(1, 2, 3, 4, 5, 7, 89, 12, 34, 67, 95, 9674)
  )

df %>% 
  group_by(x) %>% 
  mutate(res = c(NA, diff(y))) %>% 
  ungroup()
#> # A tibble: 12 x 3
#>    x              y   res
#>    <chr>      <dbl> <dbl>
#>  1 Jimmy Page     1    NA
#>  2 Jimmy Page     2     1
#>  3 Jimmy Page     3     1
#>  4 Jimmy Page     4     1
#>  5 John Smith     5    NA
#>  6 John Smith     7     2
#>  7 John Smith    89    82
#>  8 Joe Root      12    NA
#>  9 Joe Root      34    22
#> 10 Joe Root      67    33
#> 11 Joe Root      95    28
#> 12 Joe Root    9674  9579

reprex package (v2.0.1)

于 2021-09-14 创建

您可以在 ave 中使用 diff

df$diff <- ave(df$y, df$x, FUN=function(z) c(diff(z), NA))

df
#            x    y diff
#1  Jimmy Page    1    1
#2  Jimmy Page    2    1
#3  Jimmy Page    3    1
#4  Jimmy Page    4   NA
#5  John Smith    5    2
#6  John Smith    7   82
#7  John Smith   89   NA
#8    Joe Root   12   22
#9    Joe Root   34   33
#10   Joe Root   67   28
#11   Joe Root   95 9579
#12   Joe Root 9674   NA