read.csv with Dates in 3 columns

Share:

Description

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

Usage

1
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

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)

Value

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

Author(s)

Spencer Graves

See Also

read.csv Dates3to1 dateCols

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
##
## 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])
    
write.csv(cow0, "cow0.csv", row.names=FALSE)
##
## 2.  Read it 
##
cow0. <- readDates3to1("cow0.csv")

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


all.equal(cow0., cow0x)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.