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

前端 未结 2 556
一整个雨季
一整个雨季 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:36

    You could use the base function within() like so:

    mtcars <- within(mtcars, difference <- c(NA,diff(qsec)))
    

    This creates a column called "difference" with the first element NA and the rest calculated by diff(qsec).

    You could create more columns at the same time by wrapping commands in {}, such as:

    mtcars <- within(mtcars, {difference <- c(NA,diff(qsec))
                             multiple <- qsec*2})
    

    Note that you must use <- for the assignment and not =.

提交回复
热议问题