Learn R Programming

SparkR (version 2.3.0)

column_datetime_diff_functions: Date time arithmetic functions for Column operations

Description

Date time arithmetic functions defined for Column.

Usage

add_months(y, x)

datediff(y, x)

date_add(y, x)

date_format(y, x)

date_sub(y, x)

from_utc_timestamp(y, x)

months_between(y, x)

next_day(y, x)

to_utc_timestamp(y, x)

# S4 method for Column datediff(y, x)

# S4 method for Column months_between(y, x)

# S4 method for Column,character date_format(y, x)

# S4 method for Column,character from_utc_timestamp(y, x)

# S4 method for Column,character next_day(y, x)

# S4 method for Column,character to_utc_timestamp(y, x)

# S4 method for Column,numeric add_months(y, x)

# S4 method for Column,numeric date_add(y, x)

# S4 method for Column,numeric date_sub(y, x)

Arguments

y

Column to compute on.

x

For class Column, it is the column used to perform arithmetic operations with column y. For class numeric, it is the number of months or days to be added to or subtracted from y. For class character, it is

  • date_format: date format specification.

  • from_utc_timestamp, to_utc_timestamp: time zone to use.

  • next_day: day of the week string.

Details

datediff: Returns the number of days from y to x.

months_between: Returns number of months between dates y and x.

date_format: Converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument. A pattern could be for instance dd.MM.yyyy and could return a string like '18.03.1993'. All pattern letters of java.text.SimpleDateFormat can be used. Note: Use when ever possible specialized functions like year. These benefit from a specialized implementation.

from_utc_timestamp: Given a timestamp like '2017-07-14 02:40:00.0', interprets it as a time in UTC, and renders that time as a timestamp in the given time zone. For example, 'GMT+1' would yield '2017-07-14 03:40:00.0'.

next_day: Given a date column, returns the first date which is later than the value of the date column that is on the specified day of the week. For example, next_day("2015-07-27", "Sunday") returns 2015-08-02 because that is the first Sunday after 2015-07-27. Day of the week parameter is case insensitive, and accepts first three or two characters: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun".

to_utc_timestamp: Given a timestamp like '2017-07-14 02:40:00.0', interprets it as a time in the given time zone, and renders that time as a timestamp in UTC. For example, 'GMT+1' would yield '2017-07-14 01:40:00.0'.

add_months: Returns the date that is numMonths (x) after startDate (y).

date_add: Returns the date that is x days after.

date_sub: Returns the date that is x days before.

See Also

Other data time functions: column_datetime_functions

Examples

Run this code
# NOT RUN {
dts <- c("2005-01-02 18:47:22",
        "2005-12-24 16:30:58",
        "2005-10-28 07:30:05",
        "2005-12-28 07:01:05",
        "2006-01-24 00:01:10")
y <- c(2.0, 2.2, 3.4, 2.5, 1.8)
df <- createDataFrame(data.frame(time = as.POSIXct(dts), y = y))
# }
# NOT RUN {
# }
# NOT RUN {
tmp <- createDataFrame(data.frame(time_string1 = as.POSIXct(dts),
             time_string2 = as.POSIXct(dts[order(runif(length(dts)))])))
tmp2 <- mutate(tmp, datediff = datediff(tmp$time_string1, tmp$time_string2),
               monthdiff = months_between(tmp$time_string1, tmp$time_string2))
head(tmp2)
# }
# NOT RUN {
# }
# NOT RUN {
tmp <- mutate(df, from_utc = from_utc_timestamp(df$time, "PST"),
                 to_utc = to_utc_timestamp(df$time, "PST"))
head(tmp)
# }
# NOT RUN {
# }
# NOT RUN {
tmp <- mutate(df, t1 = add_months(df$time, 1),
                  t2 = date_add(df$time, 2),
                  t3 = date_sub(df$time, 3),
                  t4 = next_day(df$time, "Sun"))
head(tmp)
# }

Run the code above in your browser using DataLab