Split a character vector into individual characters? (opposite of paste or stringr::str_c)

Yes, strsplit will do it. strsplit returns a list, so you can either use unlist to coerce the string to a single character vector, or use the list index [[1]] to access first element. x <- paste(LETTERS, collapse = “”) unlist(strsplit(x, split = “”)) # [1] “A” “B” “C” “D” “E” “F” “G” “H” “I” … Read more

gsub() in R is not replacing ‘.’ (dot)

You may need to escape the . which is a special character that means “any character” (from @Mr Flick’s comment) gsub(‘\\.’, ‘-‘, x) #[1] “2014-06-09” Or gsub(‘[.]’, ‘-‘, x) #[1] “2014-06-09” Or as @Moix mentioned in the comments, we can also use fixed=TRUE instead of escaping the characters. gsub(“.”, “-“, x, fixed = TRUE)

dplyr: inner_join with a partial string match

The fuzzyjoin library has two functions regex_inner_join and fuzzy_inner_join that allow you to match partial strings: x <- data.frame(idX=1:3, string=c(“Motorcycle”, “TractorTrailer”, “Sailboat”)) y <- data.frame(idY=letters[1:3], seed=c(“ractor”, “otorcy”, “irplan”)) x$string = as.character(x$string) y$seed = as.character(y$seed) library(fuzzyjoin) x %>% regex_inner_join(y, by = c(string = “seed”)) idX string idY seed 1 1 Motorcycle b otorcy 2 2 TractorTrailer … Read more

Extracting a string between other two strings in R

You may use str_match with STR1 (.*?) STR2 (note the spaces are “meaningful”, if you want to just match anything in between STR1 and STR2 use STR1(.*?)STR2, or use STR1\\s*(.*?)\\s*STR2 to trim the value you need). If you have multiple occurrences, use str_match_all. Also, if you need to match strings that span across line breaks/newlines … Read more