r - for loop and applying condition to ignore limited data -
i have data.frame in format
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"
i wrote following loop subset data:
library (seas) uniq <- unique(unlist(mdata$id)) (i in 1: length(uniq)){ data_1 <- subset(mdata, id == uniq[i]) d1 <-mksub(data_1) year.plot(mksub(d1)) }
it works point, however, loop stops running when station id not have sufficient data. how can tell r within loop ignore station id not have sufficient data apply year.plot function , continue other station until final id?
looks thomas laid out should doing in general. let me see if can put you. going use mscdata seas package, , assume less 2 insufficient
library(seas) data(mscdata) uniq <- unique(unlist(mscdata$id)) (i in 1: length(uniq)){ data_1 <- subset(mscdata, id == uniq[i]) try(if(length(data_1) < 2) { next } else { d1 <-mksub(data_1) year.plot(mksub(d1)) }) }
this reproducible example should work you.
Comments
Post a Comment