Combine or split Date into a specified format
Usage
date3to1(data, out.format = "%Y-%m-%d", col.YMD = 1:3, as.vector = FALSE)
date1to3(
data,
in.format = "%Y-%m-%d",
date.col = 1,
out.cols = c("%Y", "%m", "%d")
)
Arguments
- data
data frame object
- out.format
date output format
- col.YMD
columns to combine for Year, Month and Day
- as.vector
return output as vector, or leave as data frame
- in.format
date input format
- date.col
numeric value of column within the dataset that contains the dates
- out.cols
cols to of date items to split. Make sure to conform to date formats. See "NOTE" section for date formats
Details
NOTE for date3to1
The three input columns corresponding to "Year Month Day" must be numeric values.
For example, Do not provide the month variable as non-numeric such as "Mar", "Jul", or "Jan".
If the values of the columns are non-numeric, the results will return an "NA" in the output.date column.
Note
DATE FORMATS IN R
Date Specification | Description | Example | ||
%a | Abbreviated weekday | Sun, Thu | ||
%A | Full weekday | Sunday | ||
%b | Abbreviated month | May, Jul | ||
%B | Full month | March, July | ||
%d | Day of the month | 27, 07 | ||
%j | Day of the year | 148, 188 | ||
%m | Month | 05, 07 | ||
%U | Week, with Sunday as first day | 22, 27 | ||
%w | Weekday, Sunday is 0 | 0, 4 | ||
%W | Week, with Monday as first day | 21, 27 | ||
%x | Date, locale-specific | |||
%y | Year without century | 84, 05 | ||
%Y | Year with century | 1984, 2005 | ||
%C | Century | 19, 20 | ||
%D | Date formatted %m/%d/%y | 07/17/23 | ||
%u | Weekday, Monday is 1 | 7, 4 |
Examples
# EXAMPLES FOR date3to1
data0 <- data.frame(y=c(NA, -1, 2001:2009),
m=c(1:2, -1, NA, 13, 2, 12, 6:9),
d=c(0, 0:6, NA, -1, 32) )
head(data0)
#> y m d
#> 1 NA 1 0
#> 2 -1 2 0
#> 3 2001 -1 1
#> 4 2002 NA 2
#> 5 2003 13 3
#> 6 2004 2 4
# combine and convert to date
# return as data frame
date3to1(data0)
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 2004-02-04
#> 7 2005 12 5 2005-12-05
#> 8 2006 6 6 2006-06-06
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
# combine and convert to date
# return as vector
date3to1(data0, as.vector = TRUE) #eg. 2004-02-04
#> [1] NA NA NA NA NA
#> [6] "2004-02-04" "2005-12-05" "2006-06-06" NA NA
#> [11] NA
# combine and convert to date in the format DD_MM_YYYY
date3to1(data0, out.format = "%d_%m_%Y") #eg. 04_02_1974
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 04_02_2004
#> 7 2005 12 5 05_12_2005
#> 8 2006 6 6 06_06_2006
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
# combine and convert to date in the format MM_DD_YY
date3to1(data0, out.format = "%m_%d_%y") #eg. 02_04_74
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 02_04_04
#> 7 2005 12 5 12_05_05
#> 8 2006 6 6 06_06_06
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
# combine and convert to date in the various date formats
date3to1(data0, out.format = "%B %d, %y") #eg. February 04, 74
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 February 04, 04
#> 7 2005 12 5 December 05, 05
#> 8 2006 6 6 June 06, 06
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
date3to1(data0, out.format = "%a, %b %d, %Y") #eg. Mon, Feb 04, 1974
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 Wed, Feb 04, 2004
#> 7 2005 12 5 Mon, Dec 05, 2005
#> 8 2006 6 6 Tue, Jun 06, 2006
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
date3to1(data0, out.format = "%A, %B %d, %Y") #eg. Monday, February 04, 1974
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 Wednesday, February 04, 2004
#> 7 2005 12 5 Monday, December 05, 2005
#> 8 2006 6 6 Tuesday, June 06, 2006
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
date3to1(data0, out.format = "Day %j in Year %Y") #eg. Day 035 in Year 1974
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 Day 035 in Year 2004
#> 7 2005 12 5 Day 339 in Year 2005
#> 8 2006 6 6 Day 157 in Year 2006
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
date3to1(data0, out.format = "Week %U in %Y") #eg. Week 05 in 1974
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 Week 05 in 2004
#> 7 2005 12 5 Week 49 in 2005
#> 8 2006 6 6 Week 23 in 2006
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
date3to1(data0, out.format = "Numeric month %m in Year %Y") #eg. Numeric month 02 in Year 1974
#> y m d output.date
#> 1 NA 1 0 <NA>
#> 2 -1 2 0 <NA>
#> 3 2001 -1 1 <NA>
#> 4 2002 NA 2 <NA>
#> 5 2003 13 3 <NA>
#> 6 2004 2 4 Numeric month 02 in Year 2004
#> 7 2005 12 5 Numeric month 12 in Year 2005
#> 8 2006 6 6 Numeric month 06 in Year 2006
#> 9 2007 7 NA <NA>
#> 10 2008 8 -1 <NA>
#> 11 2009 9 32 <NA>
# EXAMPLES FOR date1to3
data1 <- data.frame(Full.Dates =
c("2023-02-14",NA,NA,
"2002-12-04","1974-08-04",
"2008-11-10"))
head(data1)
#> Full.Dates
#> 1 2023-02-14
#> 2 <NA>
#> 3 <NA>
#> 4 2002-12-04
#> 5 1974-08-04
#> 6 2008-11-10
# split date with default settings
# return as data frame with columns
# for day(d), month(m) and year(Y)
date1to3(data1)
#> Full.Dates .date_%Y .date_%m .date_%d
#> 1 2023-02-14 2023 02 14
#> 2 <NA> <NA> <NA> <NA>
#> 3 <NA> <NA> <NA> <NA>
#> 4 2002-12-04 2002 12 04
#> 5 1974-08-04 1974 08 04
#> 6 2008-11-10 2008 11 10
# split date in the format and only return year in YYYY
date1to3(data1, out.cols = "%Y") #eg. 2002, 2023
#> Full.Dates .date_%Y
#> 1 2023-02-14 2023
#> 2 <NA> <NA>
#> 3 <NA> <NA>
#> 4 2002-12-04 2002
#> 5 1974-08-04 1974
#> 6 2008-11-10 2008
# split date in the format and only return month in m
date1to3(data1, out.cols = "%m") #eg. 02, 12, 08
#> Full.Dates .date_%m
#> 1 2023-02-14 02
#> 2 <NA> <NA>
#> 3 <NA> <NA>
#> 4 2002-12-04 12
#> 5 1974-08-04 08
#> 6 2008-11-10 11
# split date in the format and return multiple date formats colums
date1to3(data1, out.cols = c("%B","%d") )
#> Full.Dates .date_%B .date_%d
#> 1 2023-02-14 February 14
#> 2 <NA> <NA> <NA>
#> 3 <NA> <NA> <NA>
#> 4 2002-12-04 December 04
#> 5 1974-08-04 August 04
#> 6 2008-11-10 November 10
date1to3(data1, out.cols = c("%a","%b","%y") )
#> Full.Dates .date_%a .date_%b .date_%y
#> 1 2023-02-14 Tue Feb 23
#> 2 <NA> <NA> <NA> <NA>
#> 3 <NA> <NA> <NA> <NA>
#> 4 2002-12-04 Wed Dec 02
#> 5 1974-08-04 Sun Aug 74
#> 6 2008-11-10 Mon Nov 08
date1to3(data1, out.cols = c("%A","%B","%Y","%y") )
#> Full.Dates .date_%A .date_%B .date_%Y .date_%y
#> 1 2023-02-14 Tuesday February 2023 23
#> 2 <NA> <NA> <NA> <NA> <NA>
#> 3 <NA> <NA> <NA> <NA> <NA>
#> 4 2002-12-04 Wednesday December 2002 02
#> 5 1974-08-04 Sunday August 1974 74
#> 6 2008-11-10 Monday November 2008 08
date1to3(data1, out.cols = c("%j","%Y","%y","%m") )
#> Full.Dates .date_%j .date_%Y .date_%y .date_%m
#> 1 2023-02-14 045 2023 23 02
#> 2 <NA> <NA> <NA> <NA> <NA>
#> 3 <NA> <NA> <NA> <NA> <NA>
#> 4 2002-12-04 338 2002 02 12
#> 5 1974-08-04 216 1974 74 08
#> 6 2008-11-10 315 2008 08 11
date1to3(data1, out.cols = c("%U","%Y","%y","%x") )
#> Full.Dates .date_%U .date_%Y .date_%y .date_%x
#> 1 2023-02-14 07 2023 23 2/14/2023
#> 2 <NA> <NA> <NA> <NA> <NA>
#> 3 <NA> <NA> <NA> <NA> <NA>
#> 4 2002-12-04 48 2002 02 12/4/2002
#> 5 1974-08-04 31 1974 74 8/4/1974
#> 6 2008-11-10 45 2008 08 11/10/2008
date1to3(data1, out.cols = c("%m","%Y","%y","%C") )
#> Full.Dates .date_%m .date_%Y .date_%y .date_%C
#> 1 2023-02-14 02 2023 23 20
#> 2 <NA> <NA> <NA> <NA> <NA>
#> 3 <NA> <NA> <NA> <NA> <NA>
#> 4 2002-12-04 12 2002 02 20
#> 5 1974-08-04 08 1974 74 19
#> 6 2008-11-10 11 2008 08 20