I have a file with dates in the format YYYY-mm. I am trying to prepare my data for a time series analysis and therefore need to convert the formats from factor to a Date format.
What I've tried:
x <- '2011-11'
as.Date(as.character(a), "%Y-%m")
the last line gives me an output NA.
Thank you!
"2011-11" is not a date (where's the day)? Either add a day:
a <- '2011-11'
as.Date(paste0(a,'-01'))
or use zoo's yearmon class:
library(zoo)
as.yearmon(a)
'2011-11' isn't a date. Use paste(your.year.month.strs, '01', sep='-') to add the day component to your strings, then call as.Date with "%Y-%m-%d".
Related
I have a column in a dataset right now formatted for example as "Aug-19" which represents 08/01/2019 in mm/dd/yyyy format. How do I convert this into a format that can be read by R?
You can use lubridate package.
dates <- c("Aug-19", "Sep-19")
dates_myd <- paste0(dates, "-01")
lubridate::myd(dates_myd)
With lubridate we can also do
myd(dates, truncated = 1)
#[1] "2019-08-01" "2019-09-01"
data
dates <- c("Aug-19", "Sep-19")
Just a brief (and hopefully simple!) question;
I have a column of dates such as 08/14 and 08/15. However the column class is character, and I was wondering if it is possible (and if so how) to convert this into a date class? I've tried lots of different ways and it either does nothing, or returns N/A values for the whole column. Hopefully there should be a simple line of code to fix this that I haven't come across yet?
Any help much appreciated!!
One option is to create a day by pasteing and then use as.Date because date also needs day info
as.Date(paste0(c("08/14", "08/15"), "/01"), format = "%m/%y/%d")
#[1] "2014-08-01" "2015-08-01"
Or make use of as.yearmon from zoo
library(zoo)
as.Date(as.yearmon(c("08/14", "08/15"), "%m/%y"))
library(dplyr)
dates <- c("08/14", "10/13", "12/09") # year-month
datesWithYear <- paste(date, "/01", sep = "") %>%
as.Date()
If you want to use the year-month combination as a factor for cohort analysis of something similar you can always use as.yearmon() from the zoo package. tsibble and lubridate also have interesting functions for dealing with dates and time.
I have a dataframe with a date column like the following:
19870703
19920929
20051015
20030613
How can I change the numeric Data to a Date format like %Y%m%d?
Neither as.date() nor as.POSIX.ct() works?
Using lubridate
date <- ymd("19870703")
in place of date you can put your column name
as.Date("19870703", "%Y%m%d")
[1] "1987-07-03"
Apply for entire column.
library(date)
as.date(your.date.vector)
I have dates in the format 01jan2000 (without a space or any separator) and need to convert this to a date in R so I can calculate ages. I have tried both
mydata$censor_date <- as.Date(mydata$censor_date, "%d-%b-%Y")
and
mydata$censor_date <- as.Date(mydata$censor_date, "%d-%m-%Y")
But I only get NAs. I can do this in Excel, but would prefer to have one script to run rather than switch between programmes. The exact format of the date isn't important, as long as I can use R to calculate ages.
Thanks in Advance
If you have date in "01jan2000" format you not need "-" in format.
Try
as.Date(mydata$censor_date, "%d%b%Y")
I have a csv which is has a list of lat-long coordinates with dates for each. I have code to split the data by date
X <- split(mydata, mydata$date)
But when I run the function the data is sorted numerically rather than by date. So for example this:
long lat date
26.71360 -31.67479 04-04-2013
26.53347 -31.54144 05-04-2013
26.36730 -31.39950 06-04-2013
26.15438 -31.27452 04-05-2013
26.25447 -31.06523 04-05-2013
26.31591 -30.92225 04-05-2013
Would be converted to this: Note that dates are d/m/y
long lat date
26.71360 -31.67479 04-04-2013
26.53347 -31.54144 04-05-2013
26.36730 -31.39950 04-05-2013
26.15438 -31.27452 04-05-2013
26.25447 -31.06523 05-04-2013
26.31591 -30.92225 06-04-2013
How can I keep the order?
Thanks
You need to make sure that your 'date' variable is of class Date. Conversion from character to Date representations of dates can be made with as.Date. Because the format of your input dates is neither "%Y-%m-%d" nor "%Y/%m/%d" (see format argument in ?as.Date) you need to specify format explicitly. See ?strptime for abbreviations of the various time components.
df$date <- as.Date(df$date, format = "%d-%m-%Y")