I am using the following code to give me the day of the week from a date (in the form dd/mm/yyyy).
Edit: I have uploaded a more relvant dataset.
df <- structure(list(Date = c("18/01/2013", "18/01/2013", "18/01/2013",
"18/01/2013", "18/01/2013"), Time = c("07:25:30", "07:25:40",
"07:25:50", "07:26:00", "07:26:10"), Axis1 = c(217L, 320L, 821L,
18L, 40L), Steps = c(6L, 7L, 5L, 1L, 1L), wday = c(7, 7, 7, 7, 7)), .Names = c("Date", "Time", "Axis1", "Steps", "wday"), row.names = 18154:18158, class = "data.frame")
library(lubridate)
df$wday = wday(df$Date)
df$wday.name = wday(df$Date, label = TRUE, abbr = TRUE)
The 18/1 was however a Friday and not a Saturday as R reports.
Does anyone have any suggestions of how to rectify this?
EDIT: I tried to follow the suggestions given by Dirk...
as.POSIXlt(df[,1])$wday
... but this still implies that the 18/1 is a Saturday.
My timezone is GMT/UTC (+ 1 for British Summer Time), however because I just want R to read from the date column (which is just d/m/y), I presume I don't need to specify this...
How can I get a correct wday column to be added to my existing R dataframe? (as detailed previously in my original script). I am struggling to get the suggested coding working as I gave the dataframe in the wrong format - apologies.
You can use base R functions for this. Using your df
object:
R> as.POSIXlt(df[,1])$wday
[1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
R> weekdays(as.Date(df[,1]))
[1] "Friday" "Friday" "Friday" "Friday" "Friday"
[6] "Friday" "Friday" "Friday" "Friday" "Friday"
[11] "Friday" "Friday" "Friday" "Friday" "Saturday"
[16] "Saturday" "Saturday" "Saturday" "Saturday"
R>
There is a spillover into Saturday for the end because the TZ was not specified.
If you do
R> df <- data.frame(Date=seq(as.POSIXct("05:00", format="%H:%M", tz="UTC"),
+ as.POSIXct("23:00", format="%H:%M", tz="UTC"), by="hours"))
then
R> table(weekdays(as.Date(df[,1], TZ="UTC")))
Friday
19
R>
I presume the Fri/Sat error may go away under lubridate too, but I tend to use base R functions for this.
Edit: Confirmed.
R> lubridate::wday(as.Date(df[,1]), label=TRUE)
[1] Fri Fri Fri Fri Fri Fri Fri Fri Fri Fri Fri Fri Fri Fri
[15] Fri Fri Fri Fri Fri
Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
R>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With