1 Goal


The goal of this tutorial is to change the time language in R for MAC OS.


2 Data import


# First we load the libraries
library(dplyr) 
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# In this tutorial we are going to use Date and time data
Time_table <- read.csv("timetable.csv", stringsAsFactors = FALSE)
str(Time_table)
## 'data.frame':    34584 obs. of  2 variables:
##  $ Date: chr  "16/12/06" "16/12/06" "16/12/06" "16/12/06" ...
##  $ Time: chr  "18:00:00" "19:00:00" "20:00:00" "21:00:00" ...
# First we are going to create a new column containing the full date_time
Time_table <- mutate(Time_table, DateTime = paste(Date, Time))
str(Time_table)
## 'data.frame':    34584 obs. of  3 variables:
##  $ Date    : chr  "16/12/06" "16/12/06" "16/12/06" "16/12/06" ...
##  $ Time    : chr  "18:00:00" "19:00:00" "20:00:00" "21:00:00" ...
##  $ DateTime: chr  "16/12/06 18:00:00" "16/12/06 19:00:00" "16/12/06 20:00:00" "16/12/06 21:00:00" ...
# Now we transform from text to date format variable
Time_table$DateTime <- strptime(Time_table$DateTime, "%d/%m/%y %H:%M:%S")
str(Time_table$DateTime)
##  POSIXlt[1:34584], format: "2006-12-16 18:00:00" "2006-12-16 19:00:00" ...
# Finally we transform the time to POSIXct 
Time_table$DateTime <- as.POSIXct(Time_table$DateTime)
str(Time_table)
## 'data.frame':    34584 obs. of  3 variables:
##  $ Date    : chr  "16/12/06" "16/12/06" "16/12/06" "16/12/06" ...
##  $ Time    : chr  "18:00:00" "19:00:00" "20:00:00" "21:00:00" ...
##  $ DateTime: POSIXct, format: "2006-12-16 18:00:00" "2006-12-16 19:00:00" ...

3 Changing the time language in MAC OS

3.1 German


# Depending on the language set for the dates we can get the name of weekdays in different formats
# We can write the time variables in german using

Sys.setlocale("LC_TIME", "de_DE.UTF-8")
## [1] "de_DE.UTF-8"
# Now we can ask for the days of the week
unique(format(Time_table$DateTime, "%A"))
## [1] "Samstag"    "Sonntag"    "Montag"     "Dienstag"   "Mittwoch"  
## [6] "Donnerstag" "Freitag"
# We can write the short days of the week
unique(format(Time_table$DateTime, "%a"))
## [1] "Sa" "So" "Mo" "Di" "Mi" "Do" "Fr"

3.2 English


# We can write the time variables in english using
Sys.setlocale("LC_TIME", "C")
## [1] "C"
# Now we can ask for the days of the week
unique(format(Time_table$DateTime, "%A"))
## [1] "Saturday"  "Sunday"    "Monday"    "Tuesday"   "Wednesday" "Thursday" 
## [7] "Friday"
# We can write the short days of the week
unique(format(Time_table$DateTime, "%a"))
## [1] "Sat" "Sun" "Mon" "Tue" "Wed" "Thu" "Fri"

3.3 Spanish


# We can write the time variables in spanish using
Sys.setlocale("LC_TIME", "es_ES.UTF-8")
## [1] "es_ES.UTF-8"
# Now we can ask for the days of the week
unique(format(Time_table$DateTime, "%A"))
## [1] "sábado"    "domingo"   "lunes"     "martes"    "miércoles" "jueves"   
## [7] "viernes"
# We can write the short days of the week
unique(format(Time_table$DateTime, "%a"))
## [1] "sáb" "dom" "lun" "mar" "mié" "jue" "vie"

4 Conclusion


In this tutorial we have learnt how to change the language of time variables in R for MAC OS X.