-1

I have a dataset, where the data is in the format of 20110528 i.e. YYYYMMDD. I want to split the data as YYYY MM DD. can anyone help, thanks in advance.

Chetan
  • 25
  • 5

1 Answers1

3

We can do this with extract from tidyr

library(tidyr)
extract(df1, date, into = c("Year", "Month", "Day"), "(.{4})(.{2})(.{2})")

Or another option is read.csv

cbind(df1, read.csv(text=sub("(.{4})(.{2})(.{2})", "\\1,\\2,\\3",
     df1$date), header=FALSE, col.names = c("Year", "Month", "Day")))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 3
    I like using base R when possible. Thus, I like your second option more. I point this out so the OP doesn't think it's necessary to use tidyr. – rbatt Jan 23 '17 at 15:13