2016-06-29 11 views
-1

私は約2分ごとにデータポイントを持つ大きなデータフレーム(2007〜2015年)を持っています。毎週(2007年から2015年まで)のグラフをプロットして、毎週PNGファイルとして自動的にコンピュータのフォルダにエクスポートします。以前は、毎年、毎月、毎日のプロットの作業コードを正常に作成することができました。 E.g.for年間データ:週間データプロットR

for(j in 2007:2015){ 
mypath <- file.path("~", "Documents","Yearly", paste("WAO_AIR_Data_", j, ".png", sep = "")) 
png(filename = mypath, width = 963, height = 690) 
timePlot(selectByDate(new_subdata, year = j), 
     pollutant = c("CO2", "O2", "APO"), 
     date.pad = TRUE, 
     pch = c(19,19,19), 
     cex = 0.2, 
     xlab = paste("Month of year in", j), 
     ylab = "CO2, O2, and APO concentrations", 
     name.pol = c("CO2 (ppm)", "O2 (per meg)", "APO (per meg)"), 
) 
dev.off() 
} 

データフレームは、この

tail(new_subdata) 
        date  CO2  O2  APO 
1052042 2015-12-31 23:48:45 409.636 -666.39 -353.27 
1052043 2015-12-31 23:50:46 409.652 -669.62 -356.41 
1052044 2015-12-31 23:52:44 409.679 -669.44 -356.09 
1052045 2015-12-31 23:54:46 409.703 -667.07 -353.59 
1052046 2015-12-31 23:56:44 409.719 -671.02 -357.46 
1052047 2015-12-31 23:58:46 409.734  NA  NA 

のように見えます。しかし、私は毎週のプロットのためのコードを生成する方法を知りません。誰も私を助けることができますか?どうもありがとうございます! ?strptime経由

+0

ます日付変数を持つ必要があります。 'format(Sys.Date()、 '%U%Y')'はあなたに現在の週と年を与えます。 – rawr

+0

データフレームに日付変数の列があります。そして、今週を取りたいだけではなく、2007年から2015年まで毎週プロットしたいと思っています。それぞれが自動的に私のコンピュータ –

+0

@rawrにエクスポートされています。どうすればいいのか教えてください。ありがとうございました –

答えて

1

、あなたはわずかな変更であなたの例のデータを使用して%U

%U Week of the year as decimal number (00–53) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention.

x <- Sys.time() 
class(x); format(x, '%U') 
# [1] "POSIXct" "POSIXt" 
# [1] "26" 

x <- Sys.Date() 
class(x); format(x, '%U') 
# [1] "Date" 
# [1] "26" 

DateまたはPOSIXctのうちの週を取得することができます:

new_subdata <- read.table(header = TRUE, text = "date  CO2  O2  APO 
1052042 '2015-10-31 23:48:45' 409.636 -666.39 -353.27 
1052043 '2015-10-31 23:50:46' 409.652 -669.62 -356.41 
1052044 '2015-11-30 23:52:44' 409.679 -669.44 -356.09 
1052045 '2015-11-30 23:54:46' 409.703 -667.07 -353.59 
1052046 '2015-12-31 23:56:44' 409.719 -671.02 -357.46 
1052047 '2015-12-31 23:58:46' 409.734  NA  NA") 

## create a new grouping variable with year/week 
new_subdata <- within(new_subdata, { 
    yr_wk <- format(as.Date(date), '%Y %U') 
}) 

## iterate over the unique values 
jj <- unique(new_subdata$yr_wk) 
# [1] "2015 43" "2015 48" "2015 52" 

## do some plotting 
par(mfrow = n2mfrow(length(jj)), las = 1, mar = c(5,6,2,2), 
    tcl = .2, mgp = c(3,.25,0)) 
xr <- range(new_subdata$O2, na.rm = TRUE) 
yr <- range(new_subdata$CO2, na.rm = TRUE) 

for (j in jj) { 
    mypath <- file.path("~", "Documents","Yearly", sprintf("WAO_AIR_Data_%s.png", j)) 
    # png(filename = mypath, width = 963, height = 690) 
    plot(CO2 ~ O2, data = subset(new_subdata, yr_wk == j), xlim = xr, ylim = yr) 
    # dev.off() 
} 

myplot

+0

あなたが作ったコードの最後の部分が本当に好きです。それは私のコードをはるかに短くします。簡単な答えをありがとう:) –

+0

私はopenairパッケージのtimePlot関数を使用して時間ごとのデータのコードを再現しようとしました。データポイントが1つしかない時間に達すると、コードはエラーを返します。それで私を助けてくれますか?ありがとうございます! –

+0

@TungLinh上記はデータポイントが1つの例ではエラーなく実行されます。エラーは何ですか? – rawr