Learn R Programming

Ecfun (version 0.2-2)

readDates3to1: read.csv with Dates in 3 columns

Description

read.csv, converting 3-column dates into vectors of class 'Date'.

Usage

readDates3to1(file, YMD=c('Year', 'Month', 'Day'), ...)

Arguments

file

the name of a file from which the data are to be read.

YMD

Character vector of length 3 passed to dateCols

optional arguments for read.csv

Value

a data.frame with 3-column dates replace by single-column vectors of class "Date"

Details

Some files (e.g., from the Correlates of War project) have dates specified in three separate columns with names like "startMonth1", "startDay1", "startYear1", "endMonth1", ..., "endYear2". This function looks for such triples and replaces each found with a single column with a name like, "start1", "end1", ..., "end2".

ALGORITHM

1. dat <- read.csv(file, ...)

2. Dates3to1(dat, YMD)

See Also

read.csv Dates3to1 dateCols

Examples

Run this code
# NOT RUN {
##
## 1.  Write a file to be read
##
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])

cowFile <- tempfile('cow0')    
write.csv(cow0, cowFile, row.names=FALSE)
##
## 2.  Read it 
##
cow0. <- readDates3to1(cowFile)

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

# }
# NOT RUN {
all.equal(cow0., cow0x)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab