This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
enter image description here
var1 is a numeric variable
i try as.Date many times
but it did not work
I want to change the 201401(year-month) to date variable.
Dates also need a day, and you do not have that. So you need to assume which day of the month your are looking at:
dat <- data.frame(var1 = c(201401, 201402, 201403), Freq=sample(1:3))
assumed_day <- 15
dat$date <- as.Date(paste(dat$var1, assumed_day), format = "%Y%m %d")
print(dat)
# var1 Freq date
#1 201401 1 2014-01-15
#2 201402 2 2014-02-15
#3 201403 3 2014-03-15
See as.Date.
Using the format and other related function you can format the dates however you like:
dat$formatted <- paste(months(dat$date), format(dat$date, "%Y"))
print(dat)
# var1 Freq date formatted
#1 201401 1 2014-01-15 January 2014
#2 201402 3 2014-02-15 February 2014
#3 201403 2 2014-03-15 March 2014
Related
When I do this code:
library(lubridate)
df$date <- format(as.Date(df$date, "%m/%d/%y") , "%Y")
Some of the dates that are meant to be in the 1900s, eg: 1960, turn to 2060. I'm not sure how to fix this. The date range I want is 1951 - 2014, and I have around 8000 observations.
It seems that you have 2-digit years. From ?strptime
Year without century (00–99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that is the behaviour specified by the 2004 and 2008 POSIX standards, but they do also say ‘it is expected that in a future version the default century inferred from a 2-digit year will change’.
So all 2 digit years from 00-68 are prefixed with 20, hence 60 turns to 2060 and not 1960.
There could be various ways to handle this. One way would be to subtract 100 years from dates whose year is more than 2014 (since we know the range of years).
For example,
df <- data.frame(date = c('1/12/60', '1/12/78' ,'1/1/91', '1/1/54'))
df$date <- as.Date(df$date, "%m/%d/%y")
df
# date
#1 2060-01-12
#2 1978-01-12
#3 1991-01-01
#4 2054-01-01
inds <- as.numeric(format(df$date, "%Y")) > 2014
df$date[inds] <- df$date[inds] - lubridate::years(100)
df
# date
#1 1960-01-12
#2 1978-01-12
#3 1991-01-01
#4 1954-01-01
We can also do this with chron as the cut-off date is 1961 by default
as.Date(chron::dates(c('01/12/60', '01/12/78' ,'01/01/91', '01/01/54')))
#[1] "1960-01-12" "1978-01-12" "1991-01-01" "1954-01-01"
I have a data frame in which i have two columns date and days and i want to add date column with days and show the result in other column
data frame-1
col date is in format of mm/dd/yyyy format
date days
3/2/2019 8
3/5/2019 4
3/6/2019 4
3/21/2019 3
3/25/2019 7
and i want my output like this
date days new-date
3/2/2019 8 3/10/2019
3/5/2019 4 3/9/2019
3/6/2019 4 3/10/2019
3/21/2019 3 3/24/2019
3/25/2019 7 4/1/2019
i was trying this
as.Date("3/10/2019") +8
but i think it will work for a single value
Convert to actual Date values and then add Days. You need to specify the actual format of date (read ?strptime) while converting it to Date.
as.Date(df$date, "%m/%d/%Y") + df$days
#[1] "2019-03-10" "2019-03-09" "2019-03-10" "2019-03-24" "2019-04-01"
If you want the output back in same format, we can use format
df$new_date <- format(as.Date(df$date, "%m/%d/%Y") + df$days, "%m/%d/%Y")
df
# date days new_date
#1 3/2/2019 8 03/10/2019
#2 3/5/2019 4 03/09/2019
#3 3/6/2019 4 03/10/2019
#4 3/21/2019 3 03/24/2019
#5 3/25/2019 7 04/01/2019
If you get confused with different date format we can use lubridate to do
library(lubridate)
with(df, mdy(date) + days)
This question already has answers here:
Find the day of a week
(7 answers)
Closed 5 years ago.
This is my second day using R can anyone kindly tell me how to make a new column showing day of the week from an existing column which contains the dates.
my dates are in the format %d/%m/%y
I have used the following but it only shows for one day, I want it to show the whole column of dates
discount$day_of_week <-wday(as.Date('16-08-2012','%d-%m-%Y'), label=TRUE)
Can someone please kindly assist me?
example of what you might want:
df <- data.frame(id = 1:5, date = seq(Sys.Date(), length.out = 5, by = 1))
df$day_name <-weekdays(df$date)
df
# id date day_name
# 1 1 2017-09-28 Thursday
# 2 2 2017-09-29 Friday
# 3 3 2017-09-30 Saturday
# 4 4 2017-10-01 Sunday
# 5 5 2017-10-02 Monday
This question already has answers here:
How to add leading zeros?
(8 answers)
Closed 6 years ago.
I am trying to do an if else statement to say if the value is less than 10 add a zero in front, if not leave it as is. I am trying to get all of my dates to be 2 digits. Please assist.
if(df$col < 10){
paste '0'
else df$col
}
I was trying to break it down into different columns
EventID SampleDate SampleTime
130466 3/19/2008 12:30:00
131392 4/30/2008 08:45:00
131658 5/14/2008 10:00:00
117770 6/11/2008 08:45:00
118680 7/23/2008 09:15:00
118903 8/6/2008 09:00:00
SampleDatech year month day2
3/19/2008 2008 3 19
4/30/2008 2008 4 30
5/14/2008 2008 5 14
6/11/2008 2008 6 11
7/23/2008 2008 7 23
8/6/2008 2008 8 6
If you are trying to output just the day with a leading zero to a new column, you can use a combination of strftime and as.Date.
df$day = strftime(as.Date(df$SampleDate, "%m/%d/%Y"), "%d")
Or if you want to keep the whole date, but add the leading zero to the day you can do this.
df$NewDate = strftime(as.Date(df$SampleDate, "%m/%d/%y"), "%m/%d/%Y")
I have following data set:
>d
x date
1 1 1-3-2013
2 2 2-4-2010
3 3 2-5-2011
4 4 1-6-2012
I want:
> d
x date
1 1 31-12-2013
2 2 31-12-2010
3 3 31-12-2011
4 4 31-12-2012
i.e. Last day, last month and the year of the date object.
Please Help!
You can also just use the ceiling_date function in LUBRIDATE package.
You can do something like -
library(lubridate)
last_date <- ceiling_date(date,"year") - days(1)
ceiling_date(date,"year") gives you the first date of the next year and to get the last date of the current year, you subtract this by 1 or days(1).
Hope this helps.
Another option using lubridate package:
## using d from Roland answer
transform(d,last =dmy(paste0('3112',year(dmy(date)))))
x date last
1 1 1-3-2013 2013-12-31
2 2 2-4-2010 2010-12-31
3 3 2-5-2011 2011-12-31
4 4 1-6-2012 2012-12-31
d <- read.table(text="x date
1 1 1-3-2013
2 2 2-4-2010
3 3 2-5-2011
4 4 1-6-2012", header=TRUE)
d$date <- as.Date(d$date, "%d-%m-%Y")
d$date <- as.POSIXlt(d$date)
d$date$mon <- 11
d$date$mday <- 31
d$date <- as.Date(d$date)
# x date
#1 1 2013-12-31
#2 2 2010-12-31
#3 3 2011-12-31
#4 4 2012-12-31
1) cut.Date Define cut_year to give the first day of the year. Adding 366 gets us to the next year and then applying cut_year again gets us to the first day of the next year. Finally subtract 1 to get the last day of the year. The code uses base functionality only.
cut_year <- function(x) as.Date(cut(as.Date(x), "year"))
transform(d, date = cut_year(cut_year(date) + 366) - 1)
2) format
transform(d, date = as.Date(format(as.Date(date), "%Y-12-31")))
3) zoo A "yearmon" class variable stores the date as a year plus 0 for Jan, 1/12 for Feb, ..., 11/12 for Dec. Thus taking its floor and adding 11/12 gets one to Dec and as.Date.yearmon(..., frac = 1) uses the last of the month instead of the first.
library(zoo)
transform(d, date = as.Date(floor(as.yearmon(as.Date(date))) + 11 / 12, frac = 1))
Note: The inner as.Date in cut_year and in the other two solutions can be omitted if it is known that date is already of "Date" class.
ADDED additional solutions.