Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split text based on dot in R [duplicate]

Tags:

regex

r

strsplit

I have:

"word1.word2"

and I want:

"word1" "word2"

I know I have to use strsplit with perl=TRUE, but I can't find the regular expression for a period (to feed to the split argument).

like image 252
Antoine Avatar asked Mar 18 '15 15:03

Antoine


People also ask

How do you say contains in R?

In R, there is a method called contains().


2 Answers

There are several ways to do this, both with base R and with the common string processing packages (like "stringr" and "stringi").

Here are a few in base R:

str1 <- "word1.word2"

strsplit(str1, ".", fixed = TRUE)  ## Add fixed = TRUE
strsplit(str1, "[.]")              ## Make use of character classes
strsplit(str1, "\\.")              ## Escape special characters 
like image 162
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 21 '22 10:10

A5C1D2H2I1M1N2O1R2T1


Try this

library(stringr)
a <- "word1.word2"
str_split(a, "\\.")
like image 3
dimitris_ps Avatar answered Oct 21 '22 12:10

dimitris_ps