dateCols {Ecfun} | R Documentation |
Identify YMD names in a character vector
Description
grep
for YMD (year, month, day) in
col.names
. Return a named list of integer
vectors of length 3 for each triple found.
Usage
dateCols(col.names, YMD=c('Year', 'Month', 'Day'))
Arguments
col.names |
either a character vector in which to search
for names matching |
YMD |
a character vector of patterns to use in
|
Details
The data sets from the
Correlates
of War project include dates coded in
triples of columns with names like
c("StartMonth1", "StartDay1",
"StartYear1", "EndMonth1", ..., "EndYear2")
.
This function will find all relevant date
triples in a character vector of column names
and return a list of integer vectors of
length 3 with names like
"Start1", "End1", ..., "End2"
giving
the positions in col.names
of the
desired date components.
Algorithm:
1. if(!is.null(colnames(YMD)))YMD <-
colnames(YMD)
2. ymd
<- grep
for YMD
(Year, Month, Day) in col.names
.
3. groupNames
<- sub
pattern
with ” in ymd
4. Throw a warning
for any
groupNames
character string that
does not appear with all three of Year,
Month, and Day.
5. Return a list of integer vectors of length 3 for each triple found.
Value
Returns a named list of integer vectors
of length 3 identifying the positions in
col.names
of the desired date
components.
Author(s)
Spencer Graves
See Also
Examples
##
## 1. character vector
##
colNames <- c('war', 'StartMonth1',
'StartDay1', 'StartYear1',
'EndMonth1', 'EndMonth2',
'EndDay2', 'EndYear2', 'Initiator')
colNums <- dateCols(colNames)
# Should issue a warning:
# Warning message:
# In dateCols(colNames) :
# number of matches for Year = 2
# != number of matches for Month = 3
# check
colN <- list(Start1=c(Year=4, Month=2, Day=3),
End2=c(Year=8, Month=6, Day=7) )
all.equal(colNums, colN)
##
## 2. array
##
A <- matrix(ncol=length(colNames),
dimnames=list(NULL, colNames))
Anums <- dateCols(A)
# check
all.equal(Anums, colN)