0

I have a character vector that I want separated into different columns where the . is the deliminator, but the stringr::str_split(), strsplit(), and tidyr::split functions don't identify . as a deliminator, and instead give blanks instead.

test <- data.frame(data = paste(letters, '.', 1:length(letters), '.', letters, sep = ''))

str_split(string = test$data, pattern = '.', simplify = T)
separate(data = test, col = 'data', into = c('letter', 'number'), sep = '.')
strsplit(x = test$data, split = '.')

Using stringr::str_replace_all() also results in all characters within the string getting replaced as well:

str_replace_all(string = test$data, pattern = '.', replacement = '-')

My questions are:

  1. Is there a technical (e.g. computer science?) reason why it does this (just for my own curiosity)? This is so I can avoid doing this in the future and give a reason for it when I teach?
  2. How do I solve the issue of changing . to another character? Because I'm in the problem now and not sure how to resolve it...
Lalochezia
  • 497
  • 4
  • 15
  • All those functions use regular expressions and `.` is a special character for regular expressions. You can use `stringr::str_split(string = test$data, pattern = stringr::fixed('.'), simplify = T)` or `tidyr::separate_wider_delim(test, data, delim=".", names=c("letter", "number", "letter_again"))` – MrFlick Aug 31 '23 at 14:44

0 Answers0