Get weekdays in English in R
Printing of Date
and POSIX*t
objects seems to be controlled by the LC_TIME
locale category.
On Windows, you change it like this:
## First, save the current value so we can restore it later
Sys.getlocale("LC_TIME")
# [1] "English_United States.1252"
## First in Spanish
Sys.setlocale("LC_TIME","Spanish Modern Sort")
# [1] "Spanish_Spain.1252"
weekdays(Sys.Date()+0:6)
# [1] "lunes" "martes" "miércoles" "jueves" "viernes" "sábado"
# [7] "domingo"
## Then back to (US) English
Sys.setlocale("LC_TIME","English United States")
# [1] "English_United States.1252"
weekdays(Sys.Date()+0:6)
# [1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
# [7] "Sunday"
On most *NIXes, the equivalent would be:
Sys.setlocale("LC_TIME", "en_US")
The particular locale names are OS-dependent, as mentioned in ?Sys.setlocale
. For names accepted by Windows, see here. For names accepted by Linux, see here.
How to get the day of week in english not in chinese?
In the help file for strptime
:
Locale-specific conversions to and from character strings are used
where appropriate and available. This affects the names of the days
and months, the AM/PM indicator (if used) and the separators in
formats such as %x and %X (via the setting of the LC_TIME locale
category).
The minimal change would be to change LC_TIME
:
In Windows the command appears to be (from this question):
Sys.setlocale("LC_TIME", "English")
For Unix-like systems the command appears to be (from the same question):
Sys.setlocale("LC_TIME", "en_US.UTF-8")
R lubridate: weekdays in local language
@erickfis,
I created a function that generalizes your approach to any locale, as I have faced the same problem frequently:
months2<-function(date){
x<-format(ISOdate(1970, 1:12, 1), "%B")
factor(months(date),ordered=TRUE,levels=x)
}
Is there a built-in constant for the weekdays in English in base R?
Try this:
#Code
Sys.setlocale("LC_TIME", "English")
#Date
weekdays(seq(as.Date('2020-12-01'),as.Date('2020-12-07'),length.out = 7))
Output:
[1] "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday"
[7] "Monday"
Or this:
#Date2
format(seq(as.Date('2020-01-01'),as.Date('2020-12-07'),by='1 month'),'%B')
Output:
[1] "January" "February" "March" "April" "May" "June"
[7] "July" "August" "September" "October" "November" "December"
And maybe the fastest way:
#Code2
levels(lubridate::wday(Sys.Date(), label=TRUE,abbr=FALSE))
Output:
[1] "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"
[7] "Saturday"
How do I change the english weekday abbreviation?
Since you want non-standard abbreviations, you'll need to do it manually:
x <- seq(Sys.Date() - 7, Sys.Date(), by = 1)
Sys.setlocale("LC_TIME", "C") #since I'm at a non-English locale
factor(weekdays(x, TRUE),
levels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"),
labels = c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"))
#[1] Thurs Fri Sat Sun Mon Tues Wed Thurs
#Levels: Mon Tues Wed Thurs Fri Sat Sun
I'm not sure why you believe this to be related to R versions ...
Day of the Week to Return 'weekend' or 'weekday'
You can use %in%
with ifelse
to check whether the day in day_of_week
column matches c("Sat", "Sun")
vector. If it does,the ifelse
function returns weekend
. Otherwise, it returns weekday
. :
dat
# day_of_week
#1 Sat
#2 Mon
#3 Wed
#4 Sun
#5 Fri
#6 Tue
#7 Sat
#8 Wed
#9 Mon
dat$wkend_wkday <- ifelse(dat$day_of_week %in% c("Sat", "Sun"), "weekend", "weekday")
dat
# day_of_week wkend_wkday
#1 Sat weekend
#2 Mon weekday
#3 Wed weekday
#4 Sun weekend
#5 Fri weekday
#6 Tue weekday
#7 Sat weekend
#8 Wed weekday
#9 Mon weekday
Working with weekdays in number format in R lubriday::wday
You can do this efficiently without lubridate
, using days
as an index vector.
w <- tolower(weekdays(.Date(4:10)))
w
## [1] "monday" "tuesday" "wednesday" "thursday" "friday" "saturday" "sunday"
w[days]
## [1] "monday" "wednesday" "friday" "sunday"
If you want a factor, then you can do
gl(7L, 1L, labels = w)[days]
## [1] monday wednesday friday sunday
## Levels: monday tuesday wednesday thursday friday saturday sunday
optionally passing ordered = TRUE
to gl
.
It's worth mentioning that the weekdays
result is locale-dependent. Non-English locales will use non-English names for week days, etc. If you want completely reproducible results, then construct w
yourself.
w <- c("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday")
Convert day of week number to weekday name in R
Simple way with dplyr.
library(dplyr)
df <- data.frame(day_number=0:6)
df$day_number <- recode(df$day_number,
"0"="Sunday",
"1"="Monday",
"2"="Tuesday",
"3"="Wednesday",
"4"="Thursday",
"5"="Friday",
"6"="Saturday")
Fill a vector with weekdays only
There are probably a billion ways to do this with a variety of functions from multiple packages. But my first thought is to simply make a sequence and then remove the weekends:
x <- seq(as.Date('2011-01-01'),as.Date('2011-12-31'),by = 1)
x <- x[!weekdays(x) %in% c('Saturday','Sunday')]
This answer is valid only with an English based system. For instance, in a French version, 'Saturday' and 'Sunday' must be translated into 'samedi' and 'dimanche'
Related Topics
Sort Matrix According to First Column in R
Fastest Way to Multiply Matrix Columns with Vector Elements in R
Creating a Facet_Wrap Plot with Ggplot2 with Different Annotations in Each Plot
Dynamic Height and Width for Knitr Plots
How to Suppress Output When Using ':=' in R {Data.Table}, Prior to V1.8.3
Warning in Install.Packages:Installation of Package 'Tidyverse' Had Non-Zero Exit Status
How to Read CSV Data with Unknown Encoding in R
How to Create a New Column Based on Multiple Conditions from Multiple Columns
Pass String to Facet_Grid:Ggplot2
How to Add an External Legend to Ggpairs()
How to Give Color to Each Class in Scatter Plot in R
Model.Matrix() with Na.Action=Null
R Fails After Installing Gtk and Rgtk2
Fixing Set.Seed for an Entire Session