Any idea why the day is coming out wrong when the date is accurate?
I'm debugging and I can see the date variables which are correct but the day is wrong.
date Date (#9f14161)
date 26 [0x1a]
dateUTC 26 [0x1a]
day 5
dayUTC 5
fullYear 2010 [0x7da]
fullYearUTC 2010 [0x7da]
hours 17 [0x11]
hoursUTC 17 [0x11]
milliseconds 0
millisecondsUTC 0
minutes 0
minutesUTC 0
month 10 [0xa]
monthUTC 10 [0xa]
seconds 0
secondsUTC 0
time 1290790800000 [0x12c89208a80]
timezoneOffset 0
That is my date variables, as you can see, The date is 26 (today), month is 10 (this month) and the year is 2010 (this year) yet the day is coming out at 5 which is a friday.
The month begins with 0, so a month with the value 10 is not october but november.
So friday (day = 5) is correct in your example.
Related
Hello I am trying to find the week number for a series of date over three years. However R is not giving the correct week number. I am generating a seq of dates from 2016-04-01 to 2019-03-30 and then I am trying to calculate week over three years such that I get the week number 54, 55 , 56 and so on.
However when I check the week 2016-04-03 R shows the week number as 14 where as when cross checked with excel it is the week number 15 and also it simply calculates 7 days and does not reference the actual calendar days. Also the week number starts from 1 for every start of year
The code looks like this
days <- seq(as.Date("2016-04-03"),as.Date("2019-03-30"),'days')
weekdays <- data.frame('days'=days, Month = month(days), week = week(days),nweek = rep(1,length(days)))
This is how the results looks like
days week
2016-04-01 14
2016-04-02 14
2016-04-03 14
2016-04-04 14
2016-04-05 14
2016-04-06 14
2016-04-07 14
2016-04-08 15
2016-04-09 15
2016-04-10 15
2016-04-11 15
2016-04-12 15
However when checked from excel this is what I get
days week
2016-04-01 14
2016-04-02 14
2016-04-03 15
2016-04-04 15
2016-04-05 15
2016-04-06 15
2016-04-07 15
2016-04-08 15
2016-04-09 15
2016-04-10 16
2016-04-11 16
2016-04-12 16
Can someone please help me identify wherever I am going wrong.
Thanks a lot in advance!!
Not anything that you're doing wrong per se, there is just a difference in how R (I presume you're using the lubridate package) and Excel calculate week numbers.
R will calculate week numbers based on the seven day block from 1 January that year; but
Excel calculates week numbers based on a week starting from Sunday.
Taking the first few days of January 2016 for an example. On, Friday, 1 January 2016, both R and Excel will say this is week 1.
On Sunday, 3 January 2016:
this is within the first seven days of the start of the year so R will return week number 1; but
it is a Sunday, so Excel ticks over to week number 2.
Try this:
ifelse(test = weekdays.Date(days[1]) == "Sunday", yes = epiweek(days[1]), no = epiweek(days[1]) + 1) + cumsum(weekdays.Date(days) == "Sunday")
This tests whether the first day is a Sunday or not and returns an appropriate week number starting point, then adds on one more week number each Sunday. Gives the same week number if there's overlap between years.
I have a folder with several files where the name of each file is the respective userID. Something like this:
Time Sms
1 2012-01-01 00:00:00 10
2 2012-01-01 00:30:00 11
3 2012-01-01 01:00:00 13
4 2012-01-01 01:30:00 10
How can i aggretate by moth, week, hour and minute? Something like this:
Month DayofWeek hour min SMS
1 Mon 0 0 14 <-mean
1 Mon 0 30 12
1 Mon 1 0 17
1 Mon 1 30 21
.............................
12 Sunday 23 30 12
I had a similar issue aggregating hourly data into daily data. This is the code that worked for me.
fun <- function(s,i,j) { sum(s[i:(i+j-1)]) }
radday<-sapply(X=seq(1,24*nb_of_days,24),FUN=fun,s=your_time_series,j=24)
This sums data across a period j, which in my case since I was summing over 24 hours was 24. By changing the j value you can adjust it for your different periods of hour, day, week, month assuming that you have a constant period.
thanks for the help. I solved my problem by applying this code:
df<-aggregate(Sms~month(Time)+weekdays(Time)+hour(Time)+minute(Time),df,FUN='mean')
Edited for clarity:
I'm using R and I have a set of data that consists of order days:
> orders <- data.frame(order.num=1:4,
+ day = c("Mon", "Mon", "Mon", "Tue"))
> orders
order.num day
1 1 Mon
2 2 Mon
3 3 Mon
4 4 Tue
...
Orders typically come in on a consistent day (Monday in example above), but sometimes they come in on an alternate day (Tuesday in example above).
Here is actual data, spread into columns using dplyr::spread function
Outlet.number Sun Mon Tue Wed Thu Fri Sat
1 1 0 530 162 0 629 49 0
2 2 0 784 123 0 854 65 0
3 3 24 15 483 0 365 0 0
For Outlet 1, the "typical" order days are Monday and Thursday
For Outlet 2, the "typical" order days are Monday and Thursday
For Outlet 3, the "typical" order days are Tuesday and Thursday
I want to be able to predict if an order on an atypical day (e.g. Tue for Outlet 1) is more likely to be associated with the first typical day (Monday) or the second typical day (Thursday)
Neither of these examples have any orders on Wednesdays so I was able to hard code this small set, but for future outlets, Wednesday may either be a typical or atypical day.
Is there a way to ingest the data as shown above and then classify them?
If I had a sequence of weeks, does anyone know of a way to count the days in each corresponding months for a given week, using R.
So if I had a sequence :
week commencing
"2015-04-22"
"2015-04-29"
"2015-05-06"
How could I create the following:
week commencing Days in starting month Days overlapping
"2015-04-22" 7 0
"2015-04-29" 3 4
"2015-05-06" 7 0
I am aware of the lubridate package which has a number of useful associated functions but can't think how to apply them to get the above result.
dates<-as.Date(c("2015-04-22","2015-04-29" ,"2015-05-06"))
f<-Vectorize(function(d){
sum(as.POSIXlt(d+0:6)$mon==as.POSIXlt(d)$mon)
})
Result:
> data.frame(Start=dates,ThisMonth=f(dates),NextMonth=7-f(dates))
Start ThisMonth NextMonth
1 2015-04-22 7 0
2 2015-04-29 2 5
3 2015-05-06 7 0
There are a few similar queries to mine but I can't quite figure it out. In Access 2010 I have one table with three columns, day, week and number.
Day Week Number
Monday 1 12
Monday 2 24
Tuesday 2 10
Thursday 1 12
Monday 1 10
Tuesday 2 10
I want to be able to count (sum) the total "number" for Monday in Week 1, Monday in Week 2 etc.
Day Week Total
Monday 1 22
Monday 2 24
Tuesday 2 20
Thursday 1 12
You need a TOTALS Query.
SELECT
yourTable.dayFieldColumn,
yourTable.weekFieldColumn,
Sum(yourTable.numberColumnName) As TotalSum
FROM
yourTable
GROUP BY
yourTable.dayFieldColumn,
yourTable.weekFieldColumn;