Meenu Sahi
2009-Aug-04 18:12 UTC
[R] Caculate first difference from a dataframe; write a simulation
Dear R Users I'm writing my first simulation in R. I've put across my problems with a smaller example in the attachment along with the questions. Please help. Best regards Meenu -------------- next part -------------- mydat<-read.table(textConnection("Level spread change State 4.57 1.6 BlF NA 4.45 2.04 BrS NA 3.07 2.49 BlS NA 3.26 -0.26 BlF NA 2.80 0.22 BrF NA 3.22 2.5 BrS NA 4.2 -0.34 BlF NA 3.80 0.35 BrS NA 4.28 1.78 BrF NA 5.4 -0.34 BrF NA 4.89 0.24 BlF NA"), header=TRUE,as.is=TRUE) mydat3<-data.frame(mydat) q<-quantile(mydat3[[1]],c(0,0.25,0.75,1)) z.level<-cut(mydat3[[1]],q,include.lowest=T) summary(z.level) q<-quantile(mydat3[[2]],c(0,0.25,0.75,1)) z.shape<-cut(mydat3[[2]],q,include.lowest=T) summary(z.shape) #to identify States in mydat3 attach(mydat3) state1<-data.frame(mydat3[spread>=-Inf & spread<= -0.02 & Level>=-Inf & Level<=3.24,]) state2<-data.frame(mydat3[spread>-0.02 & spread<= 1.91 & Level>=-Inf & Level<=3.24,]) state3<-data.frame(mydat3[spread>1.91 & spread<= Inf & Level>=-Inf & Level<=3.24,]) state4<-data.frame(mydat3[spread>=-Inf & spread<= -0.02 & Level>3.24 & Level<=4.51,]) state5<-data.frame(mydat3[spread>-0.02 & spread<= 1.91 & Level>3.24 & Level<=4.51,]) state6<-data.frame(mydat3[spread>1.91 & spread<= Inf & Level>3.24 & Level<=4.51,]) state7<-data.frame(mydat3[spread>=-Inf & spread<= -0.02 & Level>4.51 & Level<=Inf,]) state8<-data.frame(mydat3[spread>-0.02 & spread<= 1.91 & Level>4.51 & Level<=Inf,]) state9<-data.frame(mydat3[spread>1.91 & spread<= Inf & Level>4.51 & Level<=Inf,]) detach(mydat3) state2<-transform(state2, State="State2" ) state3<-transform(state3, State="State3" ) state4<-transform(state5, State="State4" ) state5<-transform(state5, State="State5" ) state6<-transform(state6, State="State6" ) state7<-transform(state7, State="State7" ) state8<-transform(state8, State="State8" ) mydat4<-data.frame(rbind(state2,state3,state4,state5,state6,state7,state8)) ## To identify states - can it be done in an easier way? ################################################################################# ##Question1:I want to calculate the first difference of mydat4[,1:2] t<-diff(mydat4[,1:2],1) #The command fails, why? Because it fails I've written a code for calculating the first diff c<-dim(mydat4) e<-mydat4[-c[1],] dim(e) f<-mydat4[-1,] dim(f) #f firstDiff<-data.frame(f[,-c(3:4)]-e[,-c(3:4)]) # Draw a random sample for the first differences d<-dim(mydat3) z<-mydat3[d[1],1:2] z.t<-t(z) x<-data.frame(t(firstDiff[,1:2])) y<-sample(x[,1:2],1,replace=T) ad<-z.t+y ##Output of ad is given below ad X6 Level 5.04 spread 0.25 ################################################################################### ##Question2: How can I easily identify which out of the 9 states does ad fall into with Level =5.04 and spread=0.25? ##OUTPUT ad X6 Level 5.04 spread 0.25 state state8 ################################################################################## ##Question3: I want to write a simulation of the order given below: HOw can I in R store each simulation result in deltay[s,t] where s keeps track of scenarios and t keeps track of time within a scenario Do I need to declare See below in 'program-language'. For (s in 1:10000){ yield[s,1]=z.t #How can I write yield[s,]<-z.t ? For (t in 1:60){ deltay[s,t] = sample(x[,1:2],1,replace=T) yield[s,t+1]=y[s,t]+ deltay[s,t] } write scenario s # how can I write scenario s into a dataframe that can be stored for future calculations }
Gavin Simpson
2009-Aug-05 10:57 UTC
[R] Caculate first difference from a dataframe; write a simulation
On Tue, 2009-08-04 at 23:42 +0530, Meenu Sahi wrote:> Dear R Users > > I'm writing my first simulation in R. > I've put across my problems with a smaller example in the attachment along > with the questions.Q1 - mydat4[, 1:2] is not a matrix! It is a data.frame.> class(mydat4[, 1:2])[1] "data.frame"> is.matrix(mydat4[, 1:2])[1] FALSE ?diff is explicit about 'x' needing to be a numeric vector or a matrix. Cast mydat4[, 1:2] as a matrix and it will work:> diff(as.matrix(mydat4[,1:2]),1)Level spread 3 0.27 2.27 6 0.15 0.01 8 0.58 -2.15 9 0.48 1.43 81 -0.48 -1.43 91 0.48 1.43 2 0.17 0.26 10 0.95 -2.38 1 -0.83 1.94 11 0.32 -1.36 HTH G> > Please help. > > Best regards > Meenu > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090805/183f4f74/attachment-0002.bin>
David Winsemius
2009-Aug-05 13:06 UTC
[R] Caculate first difference from a dataframe; write a simulation
On Aug 4, 2009, at 2:12 PM, Meenu Sahi wrote:> Dear R Users > > I'm writing my first simulation in R. > I've put across my problems with a smaller example in the attachment > along > with the questions. > > Please help.See Simpson reply to Q1 ##Question2: How can I easily identify which out of the 9 states does ad fall into with Level =5.04 and spread=0.25? ##OUTPUT ad X6 Level 5.04 spread 0.25 state state8 But spread for state7 was -0.34????? Had it been 0.24, then this should work (some of the time.) > subset(mydat4, Level == 5.4 & spread==0.25) [1] Level spread change State <0 rows> (or 0-length row.names) Perhaps you meant to specify an "or" condition? > subset(mydat4, Level == 5.4 | spread==0.24) Level spread change State 10 5.40 -0.34 BrF State7 11 4.89 0.24 BlF State8> > Best regards > Meenu > <Example.txt>______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Heritage Laboratories West Hartford, CT