我有data.frame這種格式for循環和應用條件,忽略有限的數據
data.frame': 566171 obs. of 10 variables:
$ id : Factor w/ 120 levels "2200100","2200200",..: 1 1 1 1 1 1 1 1 1 1 ...
$ year : int 1950 1950 1950 1950 1950 1950 1950 1950 1950 1950 ...
$ yday : int 1 2 3 4 5 6 7 8 9 10 ...
$ date : Date, format: "1950-01-01" "1950-01-02" ...
$ t_max : atomic -17.2 -23.9 -25 -22.8 -19.4 -19.4 -11.1 -15.6 -17.8 -20.6 ...
..- attr(*, "long.name")= chr "daily maximum temperature"
..- attr(*, "units")= chr "°C"
$ rain : atomic 0 0 0 0 0 0 0 0 0 0 ...
..- attr(*, "long.name")= chr "total rainfall"
..- attr(*, "units")= chr "mm"
我寫了下面環路子集數據:
library (seas)
uniq <- unique(unlist(mdata$id))
for (i in 1: length(uniq)){
data_1 <- subset(mdata, id == uniq[i])
d1 <-mksub(data_1)
year.plot(mksub(d1))
}
它運作良好的但是,當車站ID沒有足夠的數據時,循環停止運行。我怎麼能告訴我的循環內的R忽略沒有足夠的數據申請year.plot功能的站ID,並繼續與其他站直到最終的ID?
你可能想要一些涉及'if'條件和'next'語句的東西。看到這裏:http://cran.r-project.org/doc/manuals/R-lang.html#Looping – Thomas 2014-09-10 20:26:05