How to separate characters from a string in R?

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

#Dia Mês  Ano
#01   01 2000

How do I do this in R?

    
asked by anonymous 15.09.2015 / 13:35

2 answers

5

Try this way:

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

or

> strsplit("01/01/2000", "/")
    
15.09.2015 / 13:42
3

For any string-related transformation I suggest stringr . The function str_split always returns a list, regardless if 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"
    
17.09.2015 / 18:09