I have a data set with date column with following format i.e. 19700101
How can I convert it as 1970-01-01 format
I tried zoo package in R but could not work it out. Can any one help me on this
library(anytime)
anydate(19700101)
Related
I have my date column formats in 01-Oct-21 and I want to change them to 01-10-2021. Does anyone knows how to do this in R?
Thanks!
Using base R. You might need to convert your original column into Date format using
old_date <- as.Date('01-Oct-21', format = '%d-%b-%y')
Then you use the format function to get into what you what
format(old_date, '%d-%m-%Y')
It will look slightly different if your dates are in a data frame.
We can use
library(lubridate)
format(dmy("01-Oct-21"), '%d-%m-%Y')
[1] "01-10-2021"
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 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 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 data frame which has the date column in format 08/21/2000(m/d/Y).
Now I want to add new column which display the date in Quarter format that is 2000-Q3.
I install zoo package and used following command. but it is giving NA value.
library(zoo)
mydf$var9=as.yearqtr("mydf$Order.Date","%m/%d/%Y").
The zoo library has created a set of functions to handle year-quarter vectors:
library(zoo)
mydf$var9=as.yearqtr(as.Date( mydf$Order.Date, "%m/%d/%Y" ).