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")
Related
I have a df with a column of the formate df$date = (1800.01, 1800.02, 1800.03) and so on.
And I can't figure out how to convert these numbers into proper monthly dates.
I tried the function date_decimal from the lubridate package, but that does not work for how my dates are formated.
Any ideas?
Probably the best way is to convert to a character first, then use one of the standard conversion functions. e.g.:
lubridate::ym(as.character(df$date))
I know it seems that this may be a repeated question, but I have tried other solutions and still cannot get it to work. I have uploaded a .csv file into r. I have done a small amount of house cleaning but ultimately I would like to convert a column from '"POSIXct" "POSIXt"' to a 'date' column type, and a 'character' column to a 'numeric'. For the latter column (change) I have decimals and --- entries, I converted the --- to NA, but fail to convert it to a 'numeric' afterwards.
df$value <- as.numeric(as.character(df$value))
I first used:
df$date <- dmy_hm(df$time_stamp, tz = "Europe/London")
to create a new date variable / column. But this did not give 'date' as a column type. I then tried using:
df$date <- as.Date(df$date)
but this did not work. Once I have converted to 'date' I need to convert the format from yyyy-mm-dd hh:mm:ss to dd/mm/yyyy.
Any help with will greatly received.
lubridate package can be wacky sometimes. Can you share head of you .csv data? you might have confused with dmy_hms with myd_hms or ymd_hms formats. Try using anytime package.
anytime::anytime(df$time_stamp)
I am using "R" and am hoping someone can assist with my date formatting issue. I have a character variable from a dataset that I Imported from Excel.
DateVar <- c("12-07-2017", "43229", "43137", "03-27-2018")
The excel file I am using has two date formats in the same variable (MM-DD-YYYY and YYYY-MM-DD), hence the two formats in "DateVar". The date formatted YYYY-MM-DD converts to the excel date (i.e 43229).
I would like to have all the values be the same date format (ideally YYYY-MM-DD), but I am having issues converting them consistently.
Your help is much appreciated.
You can create an indicator vector for the observations that have been converted wrongly:
indicator <- !grepl("-", DateVar)
Then you can use this vector to convert these dates using the answer from this - How to convert Excel date format to proper date with Lubridate
.
I'm a Rookie with R. I have read in a Data Frame from Excel in R with the read.csv2 call, (Converted the Excel-file into csv).
I changed every Date in the table to a Y-M-D Format and wanted to use:
lapply(df$dates, as.Date, Format = "%Y/%m/%d")
but it produces NAs for every Date then.
When i ask for the mode it says the Dates are "numeric".
I tried to convert into character before into Dates with:
lapply(df$dates, as.character)
I dont know why it producs the NAs. Can someone help?
If you want to avoid the pain of finding the good format, there is dataPreparation package which provide a function to do that easily.
require(dataPreparation)
df <- setColAsDate(df, cols = "dates")
It will try to guess the format among thousand of various formats.
(NB: Please note that I'm the developer of this package.)
I have looked through the site and cannot find a way to convert a date into a specific format that I need in order to properly read file names. Any help would be appreciated. I am trying to get from the standard date format
2013-06-01
to the date format of
06012013
My current attempt is
date <- as.Date("2013-06-01")
newdate <- paste(month(date),day(date),year(date))
But that leaves me with a date of
612013
How can I make sure that I keep the 0's in the new date format?
We can use format
format(date, "%m%d%Y")
#[1] "06012013"
where
date <- as.Date("2013-06-01")