Front
Back
How do you detect and replace substrings in R with stringr?
Use str_detect() to test; str_replace/all to substitute.
Code:
library(stringr)
str_detect(‘DataCamp’, ‘Camp’) # TRUE
str_replace(‘2024-01-01’, ‘-‘, ‘/’) # ‘2024/01/01’
str_replace_all(‘a-b-c’, ‘-‘, ‘_’) # ‘a_b_c’
How do you extract text with regex in stringr?
Use str_extract()/str_extract_all().
Code:
library(stringr)
str_extract_all(‘id: A12; id: B34’, ‘[A-Z][0-9]+’)
How do you split strings and trim whitespace?
Use str_split() and str_trim()/str_squish().
Code:
library(stringr)
str_squish(‘ tidy data ‘) # ‘tidy data’
How do you parse dates with lubridate? (ymd, dmy, mdy)
Use ymd()/dmy()/mdy() for common orders.
Code:
library(lubridate)
d1 <- ymd(‘2024-07-15’)
d2 <- dmy(‘15-07-2024’)
How do you handle datetimes with hours/minutes/seconds?
Use ymd_hms()/ymd_hm() etc.
Code:
library(lubridate)
ts <- ymd_hms(‘2024-07-15 12:30:00’, tz = ‘UTC’)
How do you extract date parts with lubridate?
Use year(), month(), day(), wday(), yday().
Code:
library(lubridate)
year(ymd(‘2024-07-15’)); month(ymd(‘2024-07-15’), label=TRUE)
How do you round/floor/ceiling dates?
Use floor_date(), round_date(), ceiling_date().
Code:
library(lubridate)
floor_date(ymd(‘2024-07-15’), unit=’month’)
How do you add/subtract time spans? (periods)
Use days(), months(), years() with +/-.
Code:
library(lubridate)
ymd(‘2024-07-15’) + months(1) - days(3)
How do you detect inconsistent types in columns?
Check with dplyr::summarise + across where is.character/is.numeric.
Code:
library(dplyr)
iris %>% summarise(across(everything(), class))
How do you profile missingness across columns?
Use across with is.na to count NAs per column.
Code:
library(dplyr)
df %>% summarise(across(everything(), ~ sum(is.na(.x))))
Notes:
Replace df with your data frame.