Learn R Programming

Ecfun (version 0.2-0)

countByYear: Allocate a total by year

Description

Allocate total to countByYear for a constant count per day between start and end.

Usage

countByYear(start, end, total=1)

Arguments

start, end

objects of class "Date" specifying the start, end, respectively, of the event

total

A number to be allocated by year in proportion to the number of days in the event each year.

Value

a numeric vector whose sum is total with names for all the years between start and end

Examples

Run this code
# NOT RUN {
##
## 1.  All in one year
##
start73 <- as.Date('1973-01-22')
tst1 <- countByYear(start73, start73+99, 123)

# check 
tst1. <- 123
names(tst1.) <- 1973
# }
# NOT RUN {
all.equal(tst1, tst1.)
# }
# NOT RUN {
##
## 2.  Two years 
##
tst2 <- countByYear(start73, start73+365, 123) 

# check 
dur <- 366
days1 <- (365-21)
days2 <- 22
tst2. <- 123 * c(days1, days2)/dur 
names(tst2.) <- 1973:1974 
# }
# NOT RUN {
all.equal(tst2, tst2.)
# }
# NOT RUN {
##
## 3.  Ten years 
## 
tst10 <- countByYear(start73, start73+10*365.2, 123)

# check 
days <- (c(rep(c(rep(365, 3), 366), length=10), 0)
         + c(-21, rep(0, 9), 22) )
tst10. <- 123 * days/(10*365.2+1) 
names(tst10.) <- 1973:1983 

# }
# NOT RUN {
all.equal(tst10, tst10.)
# }
# NOT RUN {
          
# }

Run the code above in your browser using DataLab