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
Related
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:
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
I have two daily time series ranging from 1st of Jan 2016 to 1st of Aug 2016, however one my my series only includes data from business days (i.e weekends and bank holidays omitted), the other has data for everyday. My question is, how do I merge the two series so that for both time series I have only the business day data left over (deleting those extra days from the second time series)
The question was tagged also with data.table so I guess that the two time series are stored as data.frames or data.tables.
By default, joins in data.table are right joins. So, if you know in advance which one the "shorter" time series is you can write:
library(data.table)
dt_long[dt_short, on = "date"]
# date weekday i.weekday
#1: 2017-03-30 4 4
#2: 2017-03-31 5 5
#3: 2017-04-03 1 1
#4: 2017-04-04 2 2
#5: 2017-04-05 3 3
#6: 2017-04-06 4 4
If you are not sure which the "shorter" time series is you can use an inner join:
dt_short[dt_long, on = "date", nomatch = 0]
nomatch = 0 specifies the inner join.
If your time series are not already data.tables as the sample data here but are stored as data.frames, you need to coerce them to data.table class beforehand by:
setDT(dt_long)
setDT(dt_short)
Data
As the OP hasn't provided any reproducible data, we need to prepare sample data on our own (similar to this answer but as data.table):
library(data.table)
dt_long <- data.table(date = as.Date("2017-03-30") + 0:7)
# add payload: integer weekday according ISO (week starts on Monday == 1L)
dt_long[, weekday := as.integer(format(date, "%u"))]
# remove weekends
dt_short <- dt_long[weekday < 6L]
We have two data.frames df_long that contains weekends and df_short that doesn't include weekends
Date <- as.Date(seq(as.Date("2003-03-03"), as.Date("2003-03-17"), by = 1), format="%Y-%m-%d")
weekday <- weekdays(as.Date(Date))
df_long <- data.frame(Date, weekday)
df_short<- df_long[ c(1:5, 8:12, 15), ]
You can join them using dplyr::inner_join to delete the weekends and holidays from df_long and keep just the business days.
library(dplyr)
df_join <- df_long %>% inner_join(., df_short, by ="Date")
> df_join
Date weekday.x weekday.y
1 2003-03-03 Monday Monday
2 2003-03-04 Tuesday Tuesday
3 2003-03-05 Wednesday Wednesday
4 2003-03-06 Thursday Thursday
5 2003-03-07 Friday Friday
6 2003-03-10 Monday Monday
7 2003-03-11 Tuesday Tuesday
8 2003-03-12 Wednesday Wednesday
9 2003-03-13 Thursday Thursday
10 2003-03-14 Friday Friday
11 2003-03-17 Monday Monday
I'm trying to define a custom week for a dataframe.
I have a dataframe with timestamps.
I've read the questions on here regarding isocalendar. While it does the job. It's not what I want.
I'm trying to define the weeks from Friday to Thrusday.
For example:
Friday 2nd Jan 2015 would be the first day of the week.
Thursday 8th Jan 2015 would be the last day of the week.
And this would be week 1.
Is there a way to set a custom weekday? so when I access the the datetime library, I get the result that I expect.
df['Week_Number'] = df['Date'].dt.week
Here's one solution - convert your dates to a Period representing weeks that end on Thursday.
In [39]: df = pd.DataFrame({'Date':pd.date_range('2015-1-1', '2015-12-31')})
In [40]: df['Period'] = df['Date'].dt.to_period('W-THU')
In [41]: df['Week_Number'] = df['Period'].dt.week
In [44]: df.head()
Out[44]:
Date Period Week_Number
0 2015-01-01 2014-12-26/2015-01-01 1
1 2015-01-02 2015-01-02/2015-01-08 2
2 2015-01-03 2015-01-02/2015-01-08 2
3 2015-01-04 2015-01-02/2015-01-08 2
4 2015-01-05 2015-01-02/2015-01-08 2
Note that it follows the same convention as datetimes, where week 1 can be incomplete, so you may have to do a little extra munging if you want 1 to be the first complete week.
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.