R subtracting 1 month from today's date gives NA
The calculation of months is indeed perfomed by base R but not the way your think. Months is used to get the month of a date object.
#Example
today <- Sys.Date()
months(today)
[1] "March"
To add or substract months, you should use %m+%
from lubridate
:
today <- Sys.Date()
today %m+% months(-1)
[1] "2017-02-28"
Subtract months from time in format year-month
As we want to subtract one month, we should subtract 1/12
which is 0.083
and not 0.1
library(zoo)
as.yearmon(global_date) - (1/12)
#[1] "Dec 2016"
If we need output in the mentioned format
format(as.yearmon(global_date) - (1/12), "%Y%m")
#[1] "201612"
Add (subtract) months without exceeding the last day of the new month
The lubridate
function %m+%
may be useful here:
Add and subtract months to a date without exceeding the last day of the new month
as.Date("2014-12-31") %m+% months(6)
# [1] "2015-06-30"
To also handle the second case, you will need to round up to nearest month using ceiling_date
, and subtract one day using days
.
ceiling_date(as.Date("2014-02-28") %m+% months(6), unit = "month") - days(1)
# [1] "2014-08-31"
How do I subtract date columns in R when date format is not recognized?
diff
is the wrong function to calculate difference between dates. You can directly subtract the dates.
t1$date_admission - t1$date_symptoms
#Time differences in days
# [1] -20 NA -172 NA 15 -1 3 11 1 -49 1 97 -1 9 101
#[16] -142 -1 -166 -116 -195 60 -18 9 0 -110 -1 -175 -80 -53 -227
#[31] -14 41 71 13 8 16 16 1 4 -176 59 29 14 102 8
#[46] -143 0 -134 91 186 0 -1 40 -25 -168 60 -83 3 36 41
#[61] -26 59 -168 -167 121 6 -111 99 -133 -1 6 -51 59 -214
You might be trying to use difftime
:
difftime(t1$date_admission, t1$date_symptoms, units = "days")
diff
function subtracts consecutive values. See for example :
diff(c(5, 9, 4, 5))
#[1] 4 -5 1
where the calculation is (9 - 5 = 4)
, (4 - 9 = -5)
and (5 - 4 = 1)
. In your case you are first subtracting the dates and then taking applying diff
on them to get difference between consecutive numbers.
Add a month to a Date
Vanilla R has a naive difftime class, but the Lubridate CRAN package lets you do what you ask:
require(lubridate)
d <- ymd(as.Date('2004-01-01')) %m+% months(1)
d
[1] "2004-02-01"
Hope that helps.
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"
Subtracting two dates of same dataset in R
X <- as.Date(df$date2) - as.Date(df$date1)
Related Topics
Clang-7: Error: Linker Command Failed With Exit Code 1 For Macos Big Sur
Read All Worksheets in an Excel Workbook into an R List With Data.Frames
Summarizing Multiple Columns With Data.Table
Manually Setting Group Colors For Ggplot2
Pass Arguments to Dplyr Functions
How to Use Facets With a Dual Y-Axis Ggplot
Why Is the Parallel Package Slower Than Just Using Apply
Chopping a String into a Vector of Fixed Width Character Elements
Summarizing by Subgroup Percentage in R
Reasons For Using the Set.Seed Function
How Can One Work Fully Generically in Data.Table in R With Column Names in Variables
Limit Ggplot2 Axes Without Removing Data (Outside Limits): Zoom
How to Delete Multiple Values from a Vector
Using the %≫% Pipe, and Dot (.) Notation
Calculate Cumulative Sum (Cumsum) by Group