Learn R Programming

Ecfun (version 0.3-2)

Dates3to1: Convert 3-column dates in data to class Date

Description

Return a data.frame with columns of class "Date" replacing all 3-column dates.

Usage

Dates3to1(data, YMD=c('Year', 'Month', 'Day'))

Value

Return a data.frame containing the information in data reformatted as described above.

Arguments

data

a data.frame assumed to include dates coded in three column sets with names matching YMD.

YMD

a character vector of length 3 of patterns to use in grep to identify triples of columns coding YMD in col.names(data).

Author

Spencer Graves

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 accept a data.frame obtained via read.csv of such a file and replace each such triple with a singe column of class 'Date' combining the triple appropriately.

See Also

dateCols Date3to1

Examples

Run this code
cow0 <- data.frame(rec=1:3, startMonth=4:6, startDay=7:9, 
    startYear=1971:1973, endMonth1=10:12, endDay1=13:15, 
    endYear1=1974:1976, txt=letters[1:3])

cow0. <- Dates3to1(cow0)

# check 
cow0x <- data.frame(rec=1:3, txt=letters[1:3], 
    start=as.Date(c('1971-04-07', '1972-05-08', '1973-06-09')), 
    end1=as.Date(c('1974-10-13', '1975-11-14', '1976-12-15')) )

stopifnot(
all.equal(cow0., cow0x)
)

Run the code above in your browser using DataLab