Have Lubridate Subtraction Return Only a Numeric Value

Have lubridate subtraction return only a numeric value

You could try using difftime instead, ie:

difftime(syrrupan$Started,syrrupan$dos1,units="days")

Note that this will give you an object of class difftime, if you want a numeric vector, wrap an as.numeric around it. Note also that you can't choose months as an option for units, but you should really stick with a time unit that has a fixed length.

Time difference in years with lubridate?

This is the lubridate approach I would take:

interval(dob, today) / years(1)

Yields the answer of 32 years.

Note that the function will complain that it cannot express the remainder of the fraction of the year. This is because year is not a fixed concept, i.e. 366 in leap years and 365 in non-leap years. You can get an answer with more detail in regard to the number of weeks and days:

interval_period = interval(dob, today)
full_year = interval_period %/% years(1)
remaining_weeks = interval_period %% years(1) %/% weeks(1)
remaining_days = interval_period %% years(1) %% weeks(1) %/% days(1)
sprintf('Your age is %d years, %d weeks and %d days', full_year, remaining_weeks, remaining_days)
# [1] "Your age is 32 years, 51 weeks and 1 days"

Note that I use %/% for division and %% as modulo to get the remaining weeks/days after subtracting the full years/weeks.

Date difference in R gives numeric value

You can use the seconds function.

library(lubridate)

dates <- c("2018-12-23 00:02:00", "2018-12-24 00:00:00", "2018-12-25 00:04:00")

ymd_hms(dates) - seconds(10)
#[1] "2018-12-23 00:01:50 UTC" "2018-12-23 23:59:50 UTC"
#[3] "2018-12-25 00:03:50 UTC"

If you want to subtract several values, possibly different, from dates you have to pass a vector to seconds.

ymd_hms(dates) - seconds(c(5, 10, 15))
#[1] "2018-12-23 00:01:55 UTC" "2018-12-23 23:59:50 UTC"
#[3] "2018-12-25 00:03:45 UTC"

R - lubridate - Convert Period into numeric counting months

Instead of using as.numeric() try time_length() from lubridate package:

period2= months(6)
time_length(period2,unit="days")
#182.6
time_length(period2,unit="weeks")
#26.09
time_length(period2,unit="months")
#6.004
class(time_length(period2,unit="months"))
#"numeric"

How to subtract years?

The easiest thing to do is to convert it into POSIXlt and subtract 2 from the years slot.

> d <- as.POSIXlt(as.Date('2010/03/17'))
> d$year <- d$year-2
> as.Date(d)
[1] "2008-03-17"

See this related question: How to subtract days in R?.

How to subtract/add days from/to a date?

Just subtract a number:

> as.Date("2009-10-01")
[1] "2009-10-01"
> as.Date("2009-10-01")-5
[1] "2009-09-26"

Since the Date class only has days, you can just do basic arithmetic on it.

If you want to use POSIXlt for some reason, then you can use it's slots:

> a <- as.POSIXlt("2009-10-04")
> names(unclass(as.POSIXlt("2009-10-04")))
[1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst"
> a$mday <- a$mday - 6
> a
[1] "2009-09-28 EDT"


Related Topics



Leave a reply



Submit