How to separate the characters of a string in R?

Asked

Viewed 3,041 times

6

I have values a string containing a date "01/01/2000" I want to separate the day, month and year.

#Data
#01/01/2000

be it

#Dia Mês  Ano
#01   01 2000

How do I do it in R?

  • I have no experience in R, but isn’t it just a split by "/" ? Look for "split in r"

  • Thanks for the tip,... I found this portal interesting: http://rfunction.com/archives/1499

2 answers

5


Try it that way:

unlist(strsplit("01/01/2000", "[/]"))

or

> strsplit("01/01/2000", "/")
  • It worked, thank you!

  • If it worked, mark the question as solved and that answer as the solution.

3

For any string-related transformation I suggest the stringr. The function str_split always returns a list, regardless of whether it receives a vector with 1 or more elements.

> stringr::str_split(rep('01/01/2000', 5), '/')
[[1]]
[1] "01"   "01"   "2000"

[[2]]
[1] "01"   "01"   "2000"

[[3]]
[1] "01"   "01"   "2000"

[[4]]
[1] "01"   "01"   "2000"

[[5]]
[1] "01"   "01"   "2000"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.