I am trying to count the number of times each weekday occurs in a dataframe. My dataframe looks like this when printed on screen (assume the columns are labeled "x")
I am currently using
sqldf("SELECT x, strftime('%w', x) FROM maxkwpeakdates")
also, if I run the code
sqldf("SELECT strftime('%w', date('now'))")
I get the right weekday.
Knowing that strftime is from sqlite and that their datetime functions are 0 based, those numbers are off. Looking at the 1st line the result should be a 3 because April 6, 2011 occurred on a Wednesday. The 10th line should also result in a 3 because January 4th 2012 also occurred on a Wednesday.
My data is actually in a dataframe stored within a shiny app which is why I am not using something like RMySQL and am instead using sqldf. I want to be able to run SQL queries on my dataframe so that I can keep the data inside the app and so that I can calculate things like average number of specific weekdays in a time period. Here is an example of my dataframe which is called maxkwpeakdates:
I did another test with my data and compared the results of using
sqldf("SELECT x, strftime('%w', x) FROM maxkwpeakdates")
and
weekdays(as.Date(maxkwpeakdates$x))
Then I stored these results in a dataframe so I could compare the results.
Notice that using as.Date reported the correct weekdays but using strftime was almost always off. Is this a problem with R talking to sqlite through strftime? Is there a way to fix this so I get the correct result with strftime?