For loop on dates in R -


r-users, have dataframe:

head(m2006)         x.id_punto   mm.gg.aa  rad_swd  2945377          1 0001-01-06  19.918   2945378          2 0001-01-06  19.911    2945379          1 0001-02-06  19.903   2945380          2 0001-02-06  19.893    2945381          1 0001-03-06  19.875  2945382          2 0001-03-06  19.858   

what need obtain different subsets every dates (mm.gg.aa):

subset(m2006, m2006$mm.gg.aa=="0001-10-06" ) 

or, in other words, different subsets every sites (x.id_punto):

subset(m2006, m2006$x.id_punto==1) 

is possible loop on sites (x.id_punto) or dates (mm.gg.aa)? have tried in way:

 output<- data.frame(id=rep(1:365))   (p in as.factor(m2006[,1]))  {               sub<-  subset(m2006, m2006$x.id_punto==p )              output[,p] <- sub$rad_swd       } 

the code run, without looping on every id. if can't loop, have write down subset(m2006, m2006$x.id_punto==xxx) thousand times... thank in advance! fra

i think description of input , desired output acheive pretty using reshape package , cast function:

require(reshape) cast( m2006 , mm.gg.aa ~ x.id_punto , value = .(rad_swd) ) #   mm.gg.aa      1      2 #1 0001-01-06 19.918 19.911 #2 0001-02-06 19.903 19.893 #3 0001-03-06 19.875 19.858 

it quicker using loops ( isn't going absolute quickest solution imagine < 1-2 seconds).


Comments