A common task performed during data preparation or data analysis is the manipulation of strings. Regular expressions are meant to assist in such and similar tasks. A regular expression is a pattern that describes a set of strings. Regular expressions can range from simple patterns (such as finding a single number) thru complex ones (such […]
Regular Expressions Solutions – Part 1
Below are the solutions to these exercises on Regular Expressions topic. #################### # # # Exercise 1 # # # #################### text1 <- “The current year is 2016” my_pattern <- “[0-9]” grepl(my_pattern,text1) ## [1] TRUE #################### # # # Exercise 2 # # # #################### string_position <- gregexpr(my_pattern,text1) string_position[[1]][1:length(string_position[[1]])] ## [1] 21 22 23 24 […]
Creating Sample Datasets – Exercises
Creating sample data is a common task performed in many different scenarios. R has several base functions that make the sampling process quite easy and fast. Below is an explanation of the main functions used in the current set of exercices: 1. set.seed() – Although R executes a random mechanism of sample creation, set.seed() function […]
Creating Sample Datasets – Solutions
Below are the solutions to these exercises on creating a sample dataset. #################### # # # Exercise 1 # # # #################### set.seed(1235) fair_coin <- sample(c(0,1), 100, replace = TRUE) #################### # # # Exercise 2 # # # #################### set.seed(2312) hourselect1 <- sample(c(8:19),10,replace=TRUE) hourselect1 ## [1] 14 19 16 18 13 15 8 10 […]
Dates and Times – Simple and Easy with lubridate Exercises (part 3)
Welcome to the third and last part of the “lubridate” exercises. If you missed Part 1 and 2 then please refer to the links below: Part 1 Part 2 In this part, I’ll cover the following topics: 1. Durations (exact spans of time) 2. Periods (relative spans of time) 3. Rounding dates As always, in […]
Dates and Times – Simple and Easy with lubridate Solutions (part 3)
Below are the solutions to these exercises on Lubridate topic. library(lubridate) #################### # # # Exercise 1 # # # #################### dseconds(160) ## [1] "160s (~2.67 minutes)" #################### # # # Exercise 2 # # # #################### dminutes(160) / 60 ## [1] "160s (~2.67 minutes)" #################### # # # Exercise 3 # # # #################### […]
Dates and Times – Simple and Easy with lubridate exercises (part 2)
This is the second part in the series teaching the “lubridate” package. As a short recap from the previous part, I mentioned that date/date_time formats are easily parced using the ymd set of functions (for example, dmy, ymd_h, etc). I also explained that arithmetic calculations are performed using the days, months, years, etc. functions. In […]
Dates and Times – Simple and Easy with lubridate solutions (part 2)
Below are the solutions to these exercises on the Lubridate topic. #################### # # # Exercise 1 # # # #################### start_date <- dmy_hms("01/12/2015 15:40:32") end_date <- dmy_hms("01/10/2016 16:01:10") #################### # # # Exercise 2 # # # #################### my_interval <- interval(start_date,end_date) #################### # # # Exercise 3 # # # #################### class(my_interval) ## [1] […]
Dates and Times – Simple and Easy with lubridate exercises (part 1)
As in any programming language, handling date and time variables can be quite frustrating, since, for example, there is no one single format for dates, there are different time zones and there are issues such as daylight saving time. Base R provides several packages for handling date and time variables, but they require mastering cumbersome […]
Dates and Times – Simple and Easy with lubridate solutions (part 1)
Below are the solutions to these exercises on dates and times – Simple lubridate Exercises. #################### # # # Exercise 1 # # # #################### start_date<- dmy("23012017") start_date ## [1] "2017-01-23" #################### # # # Exercise 2 # # # #################### today() ## [1] "2016-08-14" #################### # # # Exercise 3 # # # #################### […]