Skip to content

Instantly share code, notes, and snippets.

@rhilfi
Created December 2, 2021 14:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rhilfi/a2eecea3e59816868674109a1278bc42 to your computer and use it in GitHub Desktop.
working_with_dates_lubridate
library(tidyverse)
library(lubridate)
# you can find more information here:
# https://r4ds.had.co.nz/dates-and-times.html
# https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html
# https://lubridate.tidyverse.org
data<-read.table(text="id Tag_Monat_Jahr Monat_Tag_Jahr dmy_hms dob dod date_event date_release_white_album start_rooftop_concert end_rooftop_concert
1 28.1.2020 1.28.2020 28:01:2020:17:55:05 18.06.1942 NA 3.01.1962 22.11.1968 30.01.1969.12:30 30.01.1969.13:12
2 03.2.2019 2.3.2019 03.2.2019:09:30:10 09.10.1940 08.12.1980 3.01.1962 22.11.1968 30.01.1969.12:30 30.01.1969.13:12
3 01.12.1987 12.01.1987 01/12/1987/20:30:10 25.02.1943 29.11.2001 3.01.1962 22.11.1968 30.01.1969.12:30 30.01.1969.13:12
4 8.9.1821 9.8.1821 08:09:1821:10:45:20 7.7.1940 NA 3.01.1962 22.11.1968 30.01.1969.12:30 30.01.1969.13:12",
header=TRUE)
head(data)
# add the current date (without time)
data<-data %>%
mutate(date_exercise=today())
head(data)
# add the current date and time
data<-data %>%
mutate(date_time_exercise=now())
head(data)
# correctly format the data
str(data)
class(data$Tag_Monat_Jahr)
class(data$date_exercise)
psych::describe(data)
Hmisc::describe(data)
data$Tag_Monat_Jahr
mdy(data$Tag_Monat_Jahr) # does not work because 28 cannot be the months
data<-data %>%
mutate(Tag_Monat_Jahr=dmy(Tag_Monat_Jahr),
Monat_Tag_Jahr=mdy(Monat_Tag_Jahr),
dmy_hms=dmy_hms(dmy_hms),
dob=dmy(dob),
dod=dmy(dod),
date_event=dmy(date_event),
date_release_white_album=dmy(date_release_white_album),
start_rooftop_concert=dmy_hm(start_rooftop_concert),
end_rooftop_concert=dmy_hm(end_rooftop_concert))
# calculate year of death and age at timepoint of the creation of this exercise, as well as the age at the rooftop concert
data<-data %>%
mutate(age_at_death=time_length(interval(dob, dod), "years"),
age_at_time_of_exercise=time_length(interval(dob, date_exercise), "years"),
age_at_rooftop_concert=time_length(interval(dob, start_rooftop_concert), "years"))
# calculate duration of the rooftop concert
data<-data %>%
mutate(duration_minutes_rooftop_concert=time_length(interval(start_rooftop_concert, end_rooftop_concert), "minutes"))
# calculate duration between the release of the white album and the rooftop concert
data<-data %>%
mutate(interval_months_white_album_rooftop_concert=time_length(interval(date_release_white_album, start_rooftop_concert), "months"))
# extract year, month, day, time from the start of the rooftop concert
data<-data %>%
mutate(year_rooftop_concert=year(start_rooftop_concert),
months_rooftop_concert=month(start_rooftop_concert),
day_rooftop_concert=mday(start_rooftop_concert),
day_in_year_rooftop_concert=yday(start_rooftop_concert),
day_in_week_rooftop_concert=wday(start_rooftop_concert),
weekday_name_rooftop_concert=wday(start_rooftop_concert, label=TRUE, abbr=FALSE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment