I have a big datafile with over 1000 entries, I want to separate date into year,month and day column. I have tried using as.Date(x), it gave me an error. Strptime (x,format="%y-%m-%d"),returns MAs. Someone out the to help me.the date column is coded as "2012-Jan-03"
Asked
Active
Viewed 43 times
-1
-
Convert to date first with `df$col <- as.Date(df$col, "%Y-%b-%d")` and then use https://stackoverflow.com/questions/6550678/split-date-into-different-columns-for-year-month-and-day – Ronak Shah Jul 24 '20 at 08:19
2 Answers
0
Does this give what you want ?
library(dplyr)
df <- tibble(date = "2012-Jan-03") %>%
separate(date, into =c("year","month","day"), sep ="-")
# A tibble: 1 x 3
year month day
<chr> <chr> <chr>
1 2012 Jan 03

flafont11
- 137
- 7
0
date <- "2012-Jan-03"
df <- data.frame(
year = format(as.Date(date, "%Y-%b-%d"), "%Y"),
month = format(as.Date(date, "%Y-%b-%d"), "%b"),
day = format(as.Date(date, "%Y-%b-%d"), "%d")
)
df
year month day
1 2012 Jan 03

Chris Ruehlemann
- 20,321
- 4
- 12
- 34