如何在R中对1961年1月1日至2010年12月31日的时间段进行子集
我需要从1961年1月1日到2010年12月31日的时间段子集的帮助。我的数据是txt文件(名称如5020.txt、4050.txt、8070.txt…)。每个txt文件由日、月、年和Qmax组成如何在R中对1961年1月1日至2010年12月31日的时间段进行子集,r,loops,time,period,R,Loops,Time,Period,我需要从1961年1月1日到2010年12月31日的时间段子集的帮助。我的数据是txt文件(名称如5020.txt、4050.txt、8070.txt…)。每个txt文件由日、月、年和Qmax组成 day month year Qmax Comment 4 11 1929 50.3 -999 22 4
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
我的代码如下:
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
}
图书馆(动物园)
setwd(“H:/CLUSTERING/2cluster”)
文件名=列表文件(“./data/”)
一年生最大值试试这个:
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
输入数据:
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')
<代码> DAT可能考虑添加你所用的语言,这样人们可以帮助你在R语言中不需要它,如果<代码> z <代码>是一个带有日期类时间索引的动物园对象,那么它就把它分到指定的日期:<代码> Z0=“1960-01—01”和时间(z)谢谢,它对我有用。
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')