date3to1 {quickcode}R Documentation

Combine vector to create Date, or split Date into vector

Description

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.

Value

date derived from combining values from three columns of a data frame

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

References

Adapted from Ecfun R package

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)

# combine and convert to date
# return as data frame
date3to1(data0)

# combine and convert to date
# return as vector
date3to1(data0, as.vector = TRUE) #eg. 2004-02-04


# combine and convert to date in the format DD_MM_YYYY
date3to1(data0, out.format = "%d_%m_%Y") #eg. 04_02_1974


# combine and convert to date in the format MM_DD_YY
date3to1(data0, out.format = "%m_%d_%y") #eg. 02_04_74

# combine and convert to date in the various date formats
date3to1(data0, out.format = "%B %d, %y") #eg. February 04, 74
date3to1(data0, out.format = "%a, %b %d, %Y") #eg. Mon, Feb 04, 1974
date3to1(data0, out.format = "%A, %B %d, %Y") #eg. Monday, February 04, 1974
date3to1(data0, out.format = "Day %j in Year %Y") #eg. Day 035 in Year 1974
date3to1(data0, out.format = "Week %U in %Y") #eg. Week 05 in 1974
date3to1(data0, out.format = "Numeric month %m in Year %Y") #eg. Numeric month 02 in Year 1974





# 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)

# split date with default settings
# return as data frame with columns
# for day(d), month(m) and year(Y)
date1to3(data1)


# split date in the format and only return year in YYYY
date1to3(data1, out.cols = "%Y") #eg. 2002, 2023


# split date in the format and only return month in m
date1to3(data1, out.cols = "%m") #eg. 02, 12, 08

# split date in the format and return multiple date formats colums
date1to3(data1, out.cols = c("%B","%d") )
date1to3(data1, out.cols = c("%a","%b","%y") )
date1to3(data1, out.cols = c("%A","%B","%Y","%y") )
date1to3(data1, out.cols = c("%j","%Y","%y","%m") )
date1to3(data1, out.cols = c("%U","%Y","%y","%x") )
date1to3(data1, out.cols = c("%m","%Y","%y","%C") )


[Package quickcode version 0.8 Index]