For loop on dates in R

R-users, I have this 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 I need to do is to obtain different subsets for every dates (MM.GG.AA):

subset(M2006, M2006$MM.GG.AA=="0001-10-06" )

or, in other words, different subsets for every sites (X.ID_punto):

subset(M2006, M2006$X.ID_punto==1)

Is it possible to loop this on sites (X.ID_punto) or dates (MM.GG.AA)? I have tried in this way:

 output<- data.frame(ID=rep(1:365))
 for  (p in as.factor(M2006[,1]))  { 
             sub<-  subset(M2006, M2006$X.ID_punto==p )
             output[,p] <- sub$Rad_SWD
      }

the code run, but without looping on every ID. If I can't loop, I have to write down subset(M2006, M2006$X.ID_punto==xxx) for a thousand times... Thank you in advance! Fra

Answers


I think from your description of input and desired output you an acheive this pretty simply using the reshape package and the 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 will certainly be quicker than using loops ( it isn't going to be the absolute quickest solution but I imagine < 1-2 seconds).


Need Your Help

How to redirect to another URL if query string match?

javascript php redirect

I want to redirect www.domain.com/index.php?id=xx to www.anotherURL.com

Toggle div with easing

jquery jquery-ui html toggle easing

I've got a link, when clicking this I want a div to slideIn with easing, and then clicking the link again, it should close the div, with easing...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.