Robbie - 23 days ago 8

R Question

I have created an algorithm to create the variable

`DayOfWeek`

`InvoiceDate`

`12/1/10 8:26`

`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

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`

).

Source (Stackoverflow)

Comments