R中的时间序列数据的子集和循环

时间:2014-10-26 23:49:23

标签: r

我有一个时间序列数据集(30年)。我为月份和我想要的日期做了一个子集(如下面的代码所示)。有没有办法为每个月和那个月的日子做一个循环?另外,有没有办法在每个月对应的不同文件夹中自动保存图表?现在我通过在下面的代码中更改对应于dfOct31all <- df [ which(df$Month==10 & df$Day==31), ]的月份和日期,然后绘制并保存它来手动完成。顺便说一句,我使用的是RStudio。 有人可以指导我吗? 谢谢!

setwd("WDir")
df <- read.csv("Velocity.csv", header = TRUE)
attach(df)

#Day 31

dfOct31all <- df [ which(df$Month==10 & df$Day==31), ]
dfall31Mbs <- dfOct31all[c(-1,-2,-3)]
densities <- lapply(dfall31Mbs, density)

par(mfcol=c(5,5), oma=c(1,1,0,0), mar=c(1,1,1,0), tcl=-0.1, mgp=c(0,0,0))

plot(densities[[1]], col="black",main = "1000mb",xlab=NA,ylab=NA)
plot(densities[[2]], col="black",main="925mb",xlab=NA,ylab=NA)
plot(densities[[3]], col="black",main="850mb",xlab=NA,ylab=NA)
plot(densities[[4]], col="black",main="700mb",xlab=NA,ylab=NA)
plot(densities[[5]], col="black",main="600mb",xlab=NA,ylab=NA)
plot(densities[[6]], col="black",main="500mb",xlab=NA,ylab=NA)
plot(densities[[7]], col ="black",main="400mb",xlab=NA,ylab=NA)
plot(densities[[8]], col="black",main="300mb",xlab=NA,ylab=NA)
plot(densities[[9]], col="black",main="250mb",xlab=NA,ylab=NA)
plot(densities[[10]], col="black",main="200mb",xlab=NA,ylab=NA)
plot(densities[[11]], col= "black",main="150mb",xlab=NA,ylab=NA)
plot(densities[[12]], col= "black",main="100mb",xlab=NA,ylab=NA)
plot(densities[[13]], col = "black",main="70mb",xlab=NA,ylab=NA)
plot(densities[[14]], col="black",main="50mb",xlab=NA,ylab=NA)
plot(densities[[15]], col="black",main="30mb",xlab=NA,ylab=NA)
plot(densities[[16]], col = "black",main="20mb",xlab=NA,ylab=NA)
plot(densities[[17]], col="black",main="10mb",xlab=NA,ylab=NA)

还显示了数据片段

Year Month Day 1000mb  925mb  850mb   700mb  600mb  500mb   400mb   300mb   250mb   200mb   150mb   100mb   70mb    50mb    30mb    20mb    10mb
1984    10  31 6       6.6    7.9     11.5   14.6   17      20.8    25.8    26.4    25.3    24.4    22.7    19.9    19.2    20.4    24.8    30.8
1985    10  31 5.8     7.1    7.7     11.5   14.7   17.3    25.3    32.6    32.9    32.4    27.1    20.9    14.2    9.7     6.4     7.3     7.4
1986    10  31 4.3     6.1    7.7     11.3   18.4   26.3    34.4    44.5    48.9    46.2    34.5    20.4    13.8    13.2    21.7    31      46.4
1987    10  31 2.2     2.9    4       7      9      13.9    19.9    25.8    26.6    23.7    17.3    12      7       3.1     1.7     5.8     14.1
1988    10  31 2.5     2.1    2.3     6.5    6.4    5.1     7.4     12.1    13.4    16.1    16.7    15.2    8.8     5       2.8     6.2     8.9
1989    10  31 3.4     4      4.7     4.4    4.1    4       4.6     4.8     5.9     5.6     10.9    13.9    12.3    10.4    8.1     8       8
1990    10  31 4       4.9    7.5     14.6   19     21.9    25.7    28.3    29.4    29.2    27.3    18      12.6    10.1    9       12      19.9
1991    10  31 2.8     3.2    4       10.8   12.1   11.2    9.9     9.1     9.9     12.8    18      17.5    10.4    6.3     4.2     7.6     11.7
1992    10  31 5.9     6.9    7.9     13.1   17.9   25.2    34.6    47.3    53.3    53      42.4    21.3    11.6    6       4.6     8.5     12.8
1993    10  31 2.3     1.5    0.4     3.6    6.3    10.1    14.3    19.1    21.6    21.8    18.4    13.6    12.3    9.5     6.9     11      18.1
1994    10  31 2       2.2    3.8     11.6   17     19.8    23.6    24.9    25.5    26.2    28.4    25.2    16.7    13.6    9.3     8.3     9.8
1995    10  31 1.5     2      3.4     7.6    9.1    11.2    13.7    17.9    20.3    21.7    21.1    16.7    13      12.1    14.9    21.4    27.3
1996    10  31 1.9     2.4    3.5     8      11.7   17.4    26.4    35.6    33.3    24.6    12.4    4.1     0.5     3.4     7.2     9.4     11.6
1997    10  31 3.7     4.8    7.8     19.2   24.6   29.6    35.6    41      41.8    42      37.9    23.7    11.2    8.6     4.2     3.8     7
1998    10  31 0.7     1.1    0.9     4.8    8.4    11.4    14      25.3    29.7    25.2    15.9    6.6     2.1     1       4.5     8.9     6.1
1999    10  31 1.9     1.6    2.4     10.7   15.3   19      23.2    29      32.4    31.9    28      20.3    10.8    9.4     12      14.5    16.9
2000    10  31 5.1     5.8    6.7     12.8   18.2   23.9    29.9    40.7    42.2    33.7    23.5    12.7    2.6     1.6     3.8     4.7     5.1
2001    10  31 5.7     6.1    7.1     10.1   10.8   14.7    18.3    22.8    22.3    22.2    22      14      9.5     6.6     5.2     6.5     8.6
2002    10  31 1.4     1.6    1.8     9.2    14.5   19.5    24.8    30      30.5    27.6    22.2    13.9    9.1     7.1     8.5     16.1    23.8
2003    10  31 1.5     1.3    0.7     1      3.5    6       11.7    21.5    21.9    22.9    23      20.7    15.8    12.5    14.5    20.1    26
2004    10  31 5.4     5.6    6.9     14.4   23.3   33.3    46.1    60.9    62.1    54.6    42.9    28      17.3    12.3    10.1    13.6    13.3
2005    10  31 1.7     1.3    3       10.3   15.8   19.5    21.1    22.8    24.1    24.5    24.5    20.6    13.5    10.7    10      10.7    10.4
2006    10  31 2.3     1.5    1.7     8.7    12.5   15.9    18.7    20.5    21.8    24.3    29.9    25.3    18.3    12.8    7.7     8.8     12.4
2007    10  31 3.7     2.7    2.3     2.2    2.6    4.2     6.5     11.9    15.9    19.6    17.2    9.5     6.9     5.7     4.9     5.8     11.7
2008    10  31 7.7     10.8   14.3    20.3   23     25.8    27.4    32.1    35.4    34.8    25.8    13.2    7.1     2.9     2.6     3.4     6
2009    10  31 0.5     0.2    2       9.3    13.5   17.6    18.8    20.8    21.4    21.2    18.9    14.2    11.1    6.4     1.9     3       8
2010    10  31 5.6     6.8    8.5     13.4   16.5   20.3    23.8    26.8    31      28.1    24      15.7    9.9     7       4.8     3.9     1.8
2011    10  31 5.9     6.7    5.6     7.9    10.3   11.8    12.5    16.2    19.5    21.4    17.9    13.2    9.6     7.9     8       8.3     10.8
2012    10  31 4.8     6.3    9.4     19.5   24.2   27.2    27.5    27.3    27.7    30.7    27.5    16.7    10      7.6     8       13.8    19.7
2013    10  31 1.4     1.9    3.9     9.1    13.1   17.3    22.9    29.7    30.4    27.3    23.5    18.2    13.1    6.3     4.4     2.4     9.4

1 个答案:

答案 0 :(得分:0)

我每天都写出来而不是循环。