Robbie Robbie - 1 year ago 62
R Question

How do I create a variable "DayOfWeek" without missing a day when plotting?

I have created an algorithm to create the variable

(Monday, Tuesday, etc). When I plot it, it always misses a day somehow. Also, when plotting, the days are not in normal order (It shows: Tuesday, Friday, Sunday,etc). The variable
is like
12/1/10 8:26
. How could I fix these issues?

data$DayOfWeek <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"
, "Saturday","Friday")[as.POSIXlt(as.Date(data$InvoiceDate,"%d/%m/%Y"))$wday + 1]

Sales time series per day

timeseriesperday <- split(data$DayOfWeek, data$InvoiceNo) # Convert data in the list
timeseriesperday <- as(timeseriesperday,"transactions") # Convert dates to transcriptions
itemFrequencyPlot(timeseriesperday,type="absolute", main="Sales time series per day",
xlab="Day of the Week", ylab="Amount of Sales")

Answer Source

Have you tried to convert DayOfWeek to factor with levels in the required order?

listOfWeekdays <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
                    "Friday", "Saturday")
data$DayOfWeek <- listOfWeekdays[as.POSIXlt(as.Date(data$InvoiceDate,"%d/%m/%y"))$wday + 1]
data$DayOfWeek <- factor(data$DayOfWeek, levels = listOfWeekdays)

As pointed out by @42- the format string needs to use lower case %y for 2 digits years (e.g. 10) while upper case %Y expects 4 digits years (e.g. 2010).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download