James Steele James Steele - 1 year ago 65
R Question

Calculate & add column length_of_time from date column in R

I am trying to create a new calculated field,

. I have a column final_date with dates:


and I am trying to calculate a new field showing length of time between 10/23/2016 and final_date.

I tried using dplyr:

mutate(df, length_of_time = 10/23/2016 - final_date)

and received an error:

"Error in eval(substitute(expr), envir, enclos) :
can only subtract from "POSIXt" objects"

so then I tried to use:

df <- as.POSIXlt(df$final_date)

and again ran my original code, only to receive the following error:

Error in UseMethod("mutate_") :
no applicable method for 'mutate_' applied to an object of class "c('POSIXlt', 'POSIXt')"

Answer Source

You've got your date formats a bit confused. (see code comments for explanation)


df <- data.frame(final_date = c("2/10/2016","4/4/2016"))

## you need to specify the format of your date columns as it is ambiguous
## I've guessed you're using day/month/year
df$final_date <- as.POSIXct(df$final_date, format = "%d/%m/%Y")

## and you need to subtract the `final_date` (which is POSIXct) 
## from another POSIXct object
mutate(df, length_of_time = as.POSIXct("2016-10-23") - final_date)

final_date length_of_time
1 2016-10-02  20.95833 days
2 2016-04-04 201.95833 days

I recommend reading the documentation on dates & times to also understand the difference between POSIXct and POSIXlt, date formats, date calculations etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download