R how can I calculate difference between rows in a data frame

前端 未结 6 2014
挽巷
挽巷 2020-12-03 01:13

Here is a simple example of my problem:

> df <- data.frame(ID=1:10,Score=4*10:1)
> df
       ID Score
    1   1    40
    2   2    36
    3   3    3         


        
6条回答
  •  有刺的猬
    2020-12-03 01:45

    diff wants a matrix or a vector rather than a data frame. Try

    data.frame(diff(as.matrix(df)))
    

提交回复
热议问题