0

I need a help with subseting a time period from 1.1.1961 to 31.12.2010. My data are txt files (names are for example 5020.txt, 4050.txt,8070.txt...). Each txt file consists of day, month, year and Qmax

day           month         year          Qmax          Comment
4             11            1929          50.3         -999 
22            4             1931          74           -999 
16            10            1932          93.5         -999 
30            10            1933          14.4         -999 
15            10            1934          48.3         -999 
13            4             1935          43.3         -999 
2             6             1936          40.8         -999 
15            3             1937          86.2         -999 
23            8             1938          90.7         -999 
15            5             1939          47.9         -999 
19            10            1941          82.2         -999 
9             4             1942          41.7         -999 
9             6             1943          60.5         -999 
9             12            1944          51.5         -999 
9             2             1946          26.7         -999 
7             4             1947          22.1         -999 
7             4             1948          61.2         -999 
20            6             1949          35           -999 
26            4             1950          21.4         -999 
20            3             1951          66           -999 
22            11            1952          46.9         -999 
2             7             1954          42.8         -999 
28            3             1955          41.5         -999 
19            4             1956          27.8         -999 
18            3             1957          32.6         -999 
29            6             1958          39           -999 
2             5             1959          35           -999 
26            7             1960          74.7         -999 
21            10            1961          17.6         -999 
6             3             1962          28.3         -999 
5             10            1963          19.9         -999 
12            10            1964          15.9         -999 
11            7             1965          23.2         -999 
3             12            1966          52.7         -999 
24            12            1967          23.2         -999 
13            4             1969          18.4         -999 
19            7             1970          40.5         -999 
18            5             1972          20           -999 
3             5             1973          10.8         -999      

My code is following:

library(zoo)
setwd("H:/CLUSTERING/2cluster")

file_names = list.files("./data/")

AnnualMaxima <- vector("list", length(file_names))
names(AnnualMaxima) <- substr(file_names,1,nchar(file_names)-4)


for (i in 1:length(file_names)){

data_i = read.table(paste("./data/",file_names[i], sep=""), header = TRUE)
date_i <- as.Date(paste(data_i[,1],data_i[,2],data_i[,3]), format = "%d %m 
 %Y")
 flood_peaks_i <- zoo(data_i[,4],date_i)

 AnnualMaxima[[i]] <- flood_peaks_i

 }

My question is how to add to my loop that I want only time period from 1.1.1961 to 31.12.2010 ? any help will be appreciated. Thank you

Yuca
  • 6,010
  • 3
  • 22
  • 42

1 Answers1

0

Try this:

dat$date_i <- with(dat, as.Date(paste(year, month, day, sep="-")))
str( ind <- as.Date("1961-01-01") <= dat$date_i & dat$date_i <= as.Date("2010-12-31") )
#  logi [1:39] FALSE FALSE FALSE FALSE FALSE FALSE ...
dat <- dat[ind,]
dat
#    day month year Qmax Comment     date_i
# 29  21    10 1961 17.6    -999 1961-10-21
# 30   6     3 1962 28.3    -999 1962-03-06
# 31   5    10 1963 19.9    -999 1963-10-05
# 32  12    10 1964 15.9    -999 1964-10-12
# 33  11     7 1965 23.2    -999 1965-07-11
# 34   3    12 1966 52.7    -999 1966-12-03
# 35  24    12 1967 23.2    -999 1967-12-24
# 36  13     4 1969 18.4    -999 1969-04-13
# 37  19     7 1970 40.5    -999 1970-07-19
# 38  18     5 1972 20.0    -999 1972-05-18
# 39   3     5 1973 10.8    -999 1973-05-03

And then, as necessary, zoo-ify:

zdat <- zoo::zoo(dat$Qmax, dat$date_i)

Input data:

dat <- read.table(header=TRUE, text='
day           month         year          Qmax          Comment
4             11            1929          50.3         -999 
22            4             1931          74           -999 
16            10            1932          93.5         -999 
30            10            1933          14.4         -999 
15            10            1934          48.3         -999 
13            4             1935          43.3         -999 
2             6             1936          40.8         -999 
15            3             1937          86.2         -999 
23            8             1938          90.7         -999 
15            5             1939          47.9         -999 
19            10            1941          82.2         -999 
9             4             1942          41.7         -999 
9             6             1943          60.5         -999 
9             12            1944          51.5         -999 
9             2             1946          26.7         -999 
7             4             1947          22.1         -999 
7             4             1948          61.2         -999 
20            6             1949          35           -999 
26            4             1950          21.4         -999 
20            3             1951          66           -999 
22            11            1952          46.9         -999 
2             7             1954          42.8         -999 
28            3             1955          41.5         -999 
19            4             1956          27.8         -999 
18            3             1957          32.6         -999 
29            6             1958          39           -999 
2             5             1959          35           -999 
26            7             1960          74.7         -999 
21            10            1961          17.6         -999 
6             3             1962          28.3         -999 
5             10            1963          19.9         -999 
12            10            1964          15.9         -999 
11            7             1965          23.2         -999 
3             12            1966          52.7         -999 
24            12            1967          23.2         -999 
13            4             1969          18.4         -999 
19            7             1970          40.5         -999 
18            5             1972          20           -999 
3             5             1973          10.8         -999')
r2evans
  • 141,215
  • 6
  • 77
  • 149