No its not an or condition. Please see the changed attachment.
Many thanks for your help.
Regards
Meenu
On Wed, Aug 5, 2009 at 6:36 PM, David Winsemius <dwinsemius at
comcast.net>wrote:
>
> 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<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>
-------------- 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)
table(z.shape,z.level)
mydat3$State<-as.numeric(interaction(z.shape,z.level,sep=""))
#the clue on interaction was a great help
firstDiff<-data.frame(apply(mydat3[,1:2],2,diff))
# the clue of using apply to use diff was very helpful
#################################################################################
# Draw a random sample for the 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
ad<-data.frame(ad)
ad<-data.frame(t(ad))
##one of the Output of ad(depending on the sample result) is given below
Level spread
X1 4.77 0.68
#I want to identify which state ad lies in according to the breaks identified in
table(z.shape,z.level)
attach(ad)
state1<-data.frame(ad[spread>=-Inf & spread<= -0.02 &
Level>=2.8 & Level<=3.24,])
state2<-data.frame(ad[spread>-0.02 & spread<= 1.91 &
Level>=2.8 & Level<=3.24,])
state3<-data.frame(ad[spread>1.91 & spread<= 2.5 &
Level>=2.8 & Level<=3.24,])
state4<-data.frame(ad[spread>=-Inf & spread<= -0.02 &
Level>=3.24 & Level<=4.51,])
state5<-data.frame(ad[spread>-0.02 & spread<= 1.91 &
Level>=3.24 & Level<=4.51,])
state6<-data.frame(ad[spread>1.91 & spread<= 2.5 &
Level>=3.24 & Level<=4.51,])
state7<-data.frame(ad[spread>=-Inf & spread<= -0.02 &
Level>=4.51 & Level<=5.4,])
state8<-data.frame(ad[spread>-0.02 & spread<= 1.91 &
Level>=4.51 & Level<=5.4,])
state9<-data.frame(ad[spread>1.91 & spread<= 2.5 &
Level>=4.51 & Level<=5.4,])
detach(ad)
c<-(c(dim(state1)[1],dim(state2)[1],dim(state3)[1],dim(state4)[1],dim(state5)[1],dim(state6)[1],dim(state7)[1],dim(state8)[1],dim(state9)[1]))
c
[1] 0 0 0 0 0 0 0 0 1
c[]>0
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
# How do I extract state9 or number 9 in this particular example
##Question1: How can I identify which out of the 9 states does ad fall into with
the new sampled Level =4.77 and spread=0.68?
# desired output
Level spread State
X1 4.77 0.68 9
##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
Final array(y) of the following order:-
s t level Spread
1 1 delta[1,t,1] delta[1,t,2]
1 2
1 3
1 4
1 5
1 6
1 7
The array starts with s=1 and t=1 and draws random samples to fill in t=1 to 7.
See below in 'program-language'.
For (s in 1:100){
y[s,1]=z.t
#How can I write y[s,]<-z.t ?
For (t in 1:7){
deltay[s,t] = sample(x[,1:2],1,replace=T)
y[s,t+1]=y[s,t]+ deltay[s,t]
}
write scenario s
}