Skip to contents

Time of first or last non-NA value.

Usage

availStart(x, any = TRUE)

availEnd(x, any = TRUE)

Arguments

x

a time series or similar object

any

logical flag for multivariate objects. The default TRUE requests the first/last index containing any non-NA value. FALSE requires that all values at the first/last index must be non-NA.

Details

The time is given as a cycle-season pair.

Argument any is meaningful only for multivariate objects. Its name is short for "the first/last index for which any of the values (ie at least one) is non-NA". any = FALSE is taken to mean that the index is the first/last for which all values are non-NA.

The functions can be used together with windows to trim NA's from the beginning and/or end of the data. As an alternative we provide also methods for periodic time series methods for zoo:na.trim, see the examples below.

Value

numeric, length 2

See also

Examples

tipi <- pcts(dataFranses1996[ , "USTotalIPI"])
start(tipi)
#> [1] 1955    1
end(tipi)
#> [1] 1991    4
head(tipi)
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1955   Quarter_1 
#> Cycle:  QuarterYearCycle 
#> Number of seasons: 4 
#> 
#>       Q1 Q2 Q3 Q4
#> Y1955 NA NA NA NA
#> Y1956 NA NA      
tail(tipi)
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1990   Quarter_3 
#> Cycle:  QuarterYearCycle 
#> Number of seasons: 4 
#> 
#>       Q1    Q2    Q3    Q4   
#> Y1990             118.3 114.5
#> Y1991 111.4 112.4 116.0 113.9

tipi <- window(tipi, start = availStart(tipi), end = availEnd(tipi))
start(tipi)
#> [1] 1960    1
end(tipi)
#> [1] 1991    4
plot(tipi)


pcfr <- pcts(dataFranses1996)

pcfr2to4 <- pcfr[2:4]
head(pcfr2to4)
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         USTotalIPI CanadaUnemployment GermanyGNP
#> Y1955_1         NA                 NA         NA
#> Y1955_2         NA                 NA         NA
#> Y1955_3         NA                 NA         NA
#> Y1955_4         NA                 NA         NA
#> Y1956_1         NA                 NA         NA
#> Y1956_2         NA                 NA         NA
tail(pcfr2to4)
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         USTotalIPI CanadaUnemployment GermanyGNP
#> Y1990_3      118.3                 NA      468.9
#> Y1990_4      114.5                 NA      482.9
#> Y1991_1      111.4                 NA         NA
#> Y1991_2      112.4                 NA         NA
#> Y1991_3      116.0                 NA         NA
#> Y1991_4      113.9                 NA         NA
## time of first and last data, can be NA's
start(pcfr2to4) # 1955 Q1
#> [1] 1955    1
end(pcfr2to4)   # 1991 Q4
#> [1] 1991    4

## time of first nonNA:
availStart(pcfr[[2]]) # 1960 Q1
#> [1] 1960    1
availStart(pcfr2to4)  # 1960 Q1
#> [1] 1960    1

## time of last nonNA:
availEnd(pcfr[[2]])   # 1991 Q4
#> [1] 1991    4
availEnd(pcfr[[3]])   # 1987 Q4
#> [1] 1987    4
availEnd(pcfr[[4]])   # 1990 Q4
#> [1] 1990    4
## but at least one of them is  available for 1991 Q4, so:
availEnd(pcfr2to4)   # 1991 Q4
#> [1] 1991    4
## this requests the time of the last full record:
availEnd(pcfr2to4, any = FALSE)   # 1987 Q4
#> [1] 1987    4

pcfr2to4a <- window(pcfr2to4, start = availStart(pcfr2to4), end = availEnd(pcfr2to4))
head(pcfr2to4a)
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         USTotalIPI CanadaUnemployment GermanyGNP
#> Y1960_1       40.2                583      167.0
#> Y1960_2       39.8                427      176.2
#> Y1960_3       38.5                334      198.0
#> Y1960_4       38.0                439      190.5
#> Y1961_1       37.1                703      180.1
#> Y1961_2       39.2                480      184.9
tail(pcfr2to4a, 20)
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         USTotalIPI CanadaUnemployment GermanyGNP
#> Y1987_1      101.5               1358      388.3
#> Y1987_2      103.8               1197      402.4
#> Y1987_3      107.3               1097      417.5
#> Y1987_4      107.2               1016      431.6
#> Y1988_1      107.6                 NA      405.3
#> Y1988_2      110.0                 NA      416.6
#> Y1988_3      113.5                 NA      432.2
#> Y1988_4      112.6                 NA      446.4
#> Y1989_1      113.6                 NA      424.1
#> Y1989_2      114.5                 NA      436.3
#> Y1989_3      115.6                 NA      445.0
#> Y1989_4      114.3                 NA      460.6
#> Y1990_1      114.3                 NA      442.0
#> Y1990_2      115.5                 NA      452.1
#> Y1990_3      118.3                 NA      468.9
#> Y1990_4      114.5                 NA      482.9
#> Y1991_1      111.4                 NA         NA
#> Y1991_2      112.4                 NA         NA
#> Y1991_3      116.0                 NA         NA
#> Y1991_4      113.9                 NA         NA

## trim NA's from both ends, up to the firsxst/last full record:
pcfr2to4b <- window(pcfr2to4, start = availStart(pcfr2to4, FALSE),
                              end = availEnd(pcfr2to4, FALSE))

## TODO: need a better example here since the first non-NA value for all
##     ts in pcfr2to4 is at the same

## alternatively, use na.trim(), the default for is.na is "any"
pcpres <- window(pcts(presidents), end = c(1972, 4))

availStart(pcpres) # 1945 2
#> [1] 1945    2
availEnd(pcpres)   # 1972 2
#> [1] 1972    2

both <- na.trim(pcpres) # same as "both"
identical(na.trim(pcpres), both) # TRUE
#> [1] TRUE
head(both, 7)
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1945   Quarter_2 
#> Cycle:  QuarterYearCycle 
#> Number of seasons: 4 
#> 
#>       Q1 Q2 Q3 Q4
#> Y1945    87 82 75
#> Y1946 63 50 43 32
tail(both)
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1971   Quarter_1 
#> Cycle:  QuarterYearCycle 
#> Number of seasons: 4 
#> 
#>       Q1 Q2 Q3 Q4
#> Y1971 51 48 54 49
#> Y1972 49 61      
head(na.trim(pcpres, "left"), 7)
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1945   Quarter_2 
#> Cycle:  QuarterYearCycle 
#> Number of seasons: 4 
#> 
#>       Q1 Q2 Q3 Q4
#> Y1945    87 82 75
#> Y1946 63 50 43 32
tail(na.trim(pcpres, "right"))
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1971   Quarter_1 
#> Cycle:  QuarterYearCycle 
#> Number of seasons: 4 
#> 
#>       Q1 Q2 Q3 Q4
#> Y1971 51 48 54 49
#> Y1972 49 61      

cguk <- pcfr[c("CanadaUnemployment", "GermanyGNP", "UKTotalInvestment")]
availStart(cguk)
#> [1] 1955    1
availStart(cguk, TRUE) # same
#> [1] 1955    1

availStart(cguk, FALSE)
#> [1] 1960    1

availEnd(cguk)
#> [1] 1990    4
availEnd(cguk, TRUE) # same
#> [1] 1990    4

availEnd(cguk, FALSE)
#> [1] 1987    4


na.trim(cguk)
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         CanadaUnemployment GermanyGNP UKTotalInvestment
#> Y1960_1                583      167.0              7480
#> Y1960_2                427      176.2              7466
#> Y1960_3                334      198.0              7745
#> Y1960_4                439      190.5              8386
#> Y1961_1                703      180.1              8211
#> Y1961_2                480      184.9              8325
#> Y1961_3                325      202.9              8744
#> Y1961_4                358      195.8              8832
#> Y1962_1                561      185.4              8467
#> Y1962_2                373      195.0              8485
#> Y1962_3                282      214.5              8698
#> Y1962_4                346      204.9              8696
#> Y1963_1                546      183.0              7704
#> Y1963_2                372      199.9              8497
#> Y1963_3                272      223.5              8984
#> Y1963_4                305      215.4              9639
#> Y1964_1                463      201.6              9855
#> Y1964_2                326      214.4              9738
#> Y1964_3                243      232.8             10211
#> Y1964_4                266      226.9             10785
#> Y1965_1                397      213.4             10866
#> Y1965_2                298      226.7             10161
#> Y1965_3                210      243.4             10442
#> Y1965_4                214      239.2             11220
#> Y1966_1                303      224.3             11115
#> Y1966_2                237      235.5             10378
#> Y1966_3                236      250.0             10876
#> Y1966_4                230      240.3             11411
#> Y1967_1                340      220.3             11866
#> Y1967_2                294      232.6             11713
#> Y1967_3                260      248.3             11893
#> Y1967_4                290      248.2             12137
#> Y1968_1                411      226.9             12809
#> Y1968_2                365      243.0             11782
#> Y1968_3                332      267.1             12359
#> Y1968_4                324      267.2             13640
#> Y1969_1                405      244.0             12375
#> Y1969_2                368      262.3             11887
#> Y1969_3                329      287.1             12698
#> Y1969_4                346      286.2             13333
#> Y1970_1                455      257.4             12533
#> Y1970_2                490      280.1             12454
#> Y1970_3                479      298.2             12804
#> Y1970_4                479      298.3             13773
#> Y1971_1                601      274.5             12963
#> Y1971_2                545      286.2             12708
#> Y1971_3                494      304.8             13079
#> Y1971_4                502      302.5             13767
#> Y1972_1                591      286.0             13217
#> Y1972_2                540      296.9             12650
#> Y1972_3                536      315.2             12904
#> Y1972_4                545      318.9             13630
#> Y1973_1                599      304.5             14648
#> Y1973_2                501      312.1             13247
#> Y1973_3                476      328.7             13549
#> Y1973_4                483      328.8             14347
#> Y1974_1                564      309.4             14539
#> Y1974_2                494      314.5             12668
#> Y1974_3                488      328.4             13201
#> Y1974_4                512      324.2             14057
#> Y1975_1                738      298.2             13904
#> Y1975_2                685      308.2             12681
#> Y1975_3                670      322.1             13394
#> Y1975_4                666      329.5             13404
#> Y1976_1                776      316.1             14295
#> Y1976_2                717      328.1             12795
#> Y1976_3                709      335.2             13402
#> Y1976_4                703      348.8             13785
#> Y1977_1                909      328.6             13328
#> Y1977_2                838      334.1             12736
#> Y1977_3                826      341.8             13125
#> Y1977_4                824      358.9             14118
#> Y1978_1                999      336.5             14233
#> Y1978_2                930      346.4             13139
#> Y1978_3                878      354.8             13574
#> Y1978_4                827      370.2             13968
#> Y1979_1                966      348.3             14000
#> Y1979_2                856      361.5             13216
#> Y1979_3                759      369.8             14089
#> Y1979_4                762      384.0             15145
#> Y1980_1                953      365.3             14194
#> Y1980_2                907      365.4             12661
#> Y1980_3                816      372.8             13032
#> Y1980_4                784      381.7             13529
#> Y1981_1                951      360.9             12193
#> Y1981_2                864      364.9             11439
#> Y1981_3                840      374.8             12005
#> Y1981_4                937      384.7             12661
#> Y1982_1               1152      357.5             12423
#> Y1982_2               1266      364.8             11839
#> Y1982_3               1384      369.2             12869
#> Y1982_4               1455      379.5             13784
#> Y1983_1               1630      359.4             13485
#> Y1983_2               1515      370.4             12236
#> Y1983_3               1353      376.0             13130
#> Y1983_4               1295      393.1             14625
#> Y1984_1               1497      375.8             14552
#> Y1984_2               1430      375.6             13431
#> Y1984_3               1345      391.2             14504
#> Y1984_4               1325      405.5             15588
#> Y1985_1               1495      375.8             16000
#> Y1985_2               1353      387.2             13542
#> Y1985_3               1236      402.7             14922
#> Y1985_4               1228      412.4             15819
#> Y1986_1               1356      381.0             15393
#> Y1986_2               1245      400.1             13685
#> Y1986_3               1186      411.4             15579
#> Y1986_4               1156      422.2             16636
#> Y1987_1               1358      388.3             15956
#> Y1987_2               1197      402.4             14973
#> Y1987_3               1097      417.5             16859
#> Y1987_4               1016      431.6             18585
head( na.trim(cguk, sides = "left") )
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         CanadaUnemployment GermanyGNP UKTotalInvestment
#> Y1960_1                583      167.0              7480
#> Y1960_2                427      176.2              7466
#> Y1960_3                334      198.0              7745
#> Y1960_4                439      190.5              8386
#> Y1961_1                703      180.1              8211
#> Y1961_2                480      184.9              8325
tail( na.trim(cguk, sides = "right") )
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         CanadaUnemployment GermanyGNP UKTotalInvestment
#> Y1986_3               1186      411.4             15579
#> Y1986_4               1156      422.2             16636
#> Y1987_1               1358      388.3             15956
#> Y1987_2               1197      402.4             14973
#> Y1987_3               1097      417.5             16859
#> Y1987_4               1016      431.6             18585

head( na.trim(cguk, sides = "left", is.na = "all") )
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         CanadaUnemployment GermanyGNP UKTotalInvestment
#> Y1955_1                 NA         NA              5654
#> Y1955_2                 NA         NA              5720
#> Y1955_3                 NA         NA              5925
#> Y1955_4                 NA         NA              6469
#> Y1956_1                 NA         NA              6019
#> Y1956_2                 NA         NA              6019
tail( na.trim(cguk, sides = "right", is.na = "all") )
#> An object of class "PeriodicMTS"
#> Slot "cycle": Object from built-in class 'QuarterYearCycle'
#> Cycle start: Quarter_1 
#> 
#>         CanadaUnemployment GermanyGNP UKTotalInvestment
#> Y1989_3                 NA      445.0                NA
#> Y1989_4                 NA      460.6                NA
#> Y1990_1                 NA      442.0                NA
#> Y1990_2                 NA      452.1                NA
#> Y1990_3                 NA      468.9                NA
#> Y1990_4                 NA      482.9                NA