SparkR (version 2.1.2)

lag: lag

Description

Window function: returns the value that is offset rows before the current row, and defaultValue if there is less than offset rows before the current row. For example, an offset of one will return the previous row at any given point in the window partition.

Usage

lag(x, ...)

# S4 method for characterOrColumn lag(x, offset = 1, defaultValue = NULL)

Arguments

x

the column as a character string or a Column to compute on.

...

further arguments to be passed to or from other methods.

offset

the number of rows back from the current row from which to obtain a value. If not specified, the default is 1.

defaultValue

(optional) default to use when the offset row does not exist.

Details

This is equivalent to the LAG function in SQL.

See Also

Other window_funcs: cume_dist, dense_rank, lead, ntile, percent_rank, rank, row_number

Examples

Run this code
# NOT RUN {
  df <- createDataFrame(mtcars)

  # Partition by am (transmission) and order by hp (horsepower)
  ws <- orderBy(windowPartitionBy("am"), "hp")

  # Lag mpg values by 1 row on the partition-and-ordered table
  out <- select(df, over(lag(df$mpg), ws), df$mpg, df$hp, df$am)
# }

Run the code above in your browser using DataLab