Adding new column with diff() function when there is one less row in R

前端 未结 2 562
一整个雨季
一整个雨季 2020-12-10 20:41

If I have a sample data frame like mtcars, and I want to find the difference between mtcars$qsec for all rows, I can do diff(mtcars$qsec). But is there a simple way to make

2条回答
  •  一个人的身影
    2020-12-10 21:47

    Here are two approaches. Both put an NA in the first row of diff_qsec and put diff(qsec) in the remaining rows:

    library(dplyr)  
    mtcars %>% mutate(diff_qsec = qsec - lag(qsec)) # dplyr has its own version of lag
    
    transform(mtcars, diff_qsec = c(NA, diff(qsec)))
    

    Also, on the general issue of padding see: How can I pad a vector with NA from the front?

提交回复
热议问题