Last data update: 2014.03.03

R: read.csv with Dates in 3 columns
readDates3to1R Documentation

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

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


Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Ecfun)

Attaching package: 'Ecfun'

The following object is masked from 'package:base':

    sign

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Ecfun/readDates3to1.Rd_%03d_medium.png", width=480, height=480)
> ### Name: readDates3to1
> ### Title: read.csv with Dates in 3 columns
> ### Aliases: readDates3to1
> ### Keywords: IO
> 
> ### ** Examples
> 
> ##
> ## 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')) )
> 
> ## Don't show: 
> stopifnot(
+ ## End(Don't show)
+ all.equal(cow0., cow0x)
+ ## Don't show: 
+ )
> ## End(Don't show)
> 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>