Re: [R] writing a simulation
Dear All I 've solved this problem... Thanks Meenu On Thu, Aug 6, 2009 at 1:28 AM, Meenu Sahi meenus...@gmail.com wrote: 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 dwinsem...@comcast.netwrote: 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.34BrF State7 11 4.89 0.24BlF State8 Best regards Meenu Example.txt__ R-help@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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD Heritage Laboratories West Hartford, CT [[alternative HTML version deleted]] __ R-help@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.
[R] Write the results of sample function which uses a dataframe into an Array
Dear All? Basically I want to write the results of the sample function into an array is a specific order. The sample function uses a dataframe x.1 and therefore the result is also a dataframe and so I cannot write the result into the array in the correct order. If I convert x.1 into a matrix then the sample function fails. PLease help as to what is the problem !!! Has anyone had a similar problem? Please read the question and the problem in the attachment. Many thanks for your time. Kind regards Meenu x.1 is a matrix with 93 columns class(x.1) [1] matrix x.1 41 4243 44 45 EU0006MIndex 0.00500 -0.03337 1.431e-02 0.01387 -0.05793000 DMSW1Curncy 0.06420 -0.05780 5.610e-02 -0.00060 -0.1517 DMSW2Curncy 0.09600 -0.06450 1.053e-01 -0.02730 -0.2335 DMSW3Curncy 0.05850 -0.07550 1.398e-01 -0.05470 -0.2466 DMSW4Curncy 0.02650 -0.08650 1.710e-01 -0.08500 -0.2365 DMSW5Curncy -0.01100 -0.09400 1.815e-01 -0.09830 -0.2252 DMSW6Curncy -0.03650 -0.10100 1.795e-01 -0.09840 -0.2181 DMSW7Curncy -0.05650 -0.11750 1.864e-01 -0.09590 -0.2100 DMSW8Curncy -0.07300 -0.12250 1.872e-01 -0.09920 -0.2015 DMSW9Curncy -0.08650 -0.12650 1.876e-01 -0.09860 -0.1915 DMSW10Curncy -0.10750 -0.13550 1.872e-01 -0.09270 -0.1850 DMSW12Curncy -0.11950 -0.14650 1.900e-01 -0.09300 -0.1735 DMSW15Curncy -0.13200 -0.16600 1.790e-01 -0.07900 -0.1580 DMSW20Curncy -0.13050 -0.19100 1.560e-01 -0.07450 -0.1395 DMSW30Curncy -0.13150 -0.22800 1.600e-01 -0.06900 -0.1230 EUSA40Curncy -0.13350 -0.23220 1.552e-01 -0.07950 -0.1015 EUSA50Curncy -0.13250 -0.22970 1.637e-01 -0.08950 -0.1005 Libor_OAS 0.001208618 -0.005033614 4.506e-05 -0.004526479 -0.01386261 46 47 48 49 50 EU0006MIndex -0.00938 -0.04000 0.04888 0.00687 0.05225 DMSW1Curncy -0.06020 -0.07430 0.15850 -0.02080 0.12740 DMSW2Curncy -0.10200 -0.13300 0.24260 -0.06270 0.18310 DMSW3Curncy -0.12900 -0.13990 0.23590 -0.08500 0.17900 DMSW4Curncy -0.16600 -0.13990 0.21640 -0.09800 0.16670 DMSW5Curncy -0.14700 -0.14990 0.18240 -0.11400 0.14700 DMSW6Curncy -0.13400 -0.15650 0.15500 -0.12700 0.12500 DMSW7Curncy -0.18100 -0.14000 0.16750 -0.13300 0.10400 DMSW8Curncy -0.17300 -0.12650 0.13400 -0.14000 0.08800 DMSW9Curncy -0.16200 -0.14990 0.13640 -0.14500 0.07550 DMSW10Curncy -0.14650 -0.16150 0.12650 -0.14800 0.06650 DMSW12Curncy -0.14100 -0.16000 0.11750 -0.15300 0.05450 DMSW15Curncy -0.13100 -0.16700 0.10300 -0.15500 0.04400 DMSW20Curncy -0.11300 -0.17000 0.08400 -0.16150 0.03450 DMSW30Curncy -0.10900 -0.17990 0.07490 -0.16800 0.02200 EUSA40Curncy -0.11000 -0.18500 0.07500 -0.17400 0.03250 EUSA50Curncy -0.11200 -0.18500 0.07800 -0.17700 0.03150 Libor_OAS-0.002243875 0.006692975 -0.002139178 0.006372163 -0.000319492 51 52 53 54 90 EU0006MIndex 0.17200 0.2200 0.03963000 0.0595 -0.7918700 DMSW1Curncy 0.24500 0.2143 0.0662 0.0640 -0.460 DMSW2Curncy 0.27230 0.1552 0.0750 0.1170 -0.422 DMSW3Curncy 0.27300 0.1203 0.0742 0.1420 -0.386 DMSW4Curncy 0.28730 0.1020 0.0335 0.1621 -0.311 DMSW5Curncy 0.28650 0.0917 -0.0037 0.1786 -0.2255000 DMSW6Curncy 0.28350 0.0787 -0.0297 0.1849 -0.164 DMSW7Curncy 0.27050 0.0711 -0.0591 0.1957 -0.109 DMSW8Curncy 0.26050 0.0611 -0.0741 0.1939 -0.050 DMSW9Curncy 0.25300 0.0522 -0.0942 0.1972 -0.041 DMSW10Curncy 0.24500 0.0463 -0.1083 0.1975 -0.0175000 DMSW12Curncy 0.23000 0.0400 -0.1270 0.1926 0.0255000 DMSW15Curncy 0.21100 0.0388 -0.1418 0.1811 0.0885000 DMSW20Curncy 0.19000 0.0325 -0.1545 0.1825 0.1165000 DMSW30Curncy 0.19100 0.0215 -0.1765 0.1850 0.1295000 EUSA40Curncy 0.17450 0.0085 -0.1835 0.1800 0.1145000 EUSA50Curncy 0.17250 0.0045 -0.1895 0.1680 0.0805000 Libor_OAS-0.006313754 -0.01380074 -0.01809107 -0.01037939 0.2329775 91 92 93 94 20
Re: [R] writing a simulation
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 dwinsem...@comcast.netwrote: 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.34BrF State7 11 4.89 0.24BlF State8 Best regards Meenu Example.txt__ R-help@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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD Heritage Laboratories West Hartford, CT 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[spread1.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[spread1.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[spread1.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 } __ R-help@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.
Re: [R] Add columns in a dataframe and fill them from another table according to a criteria
Thank you very much !!! It works !!! Sorry it took me some time to understand your solution and apply it to my extended dataset. Just got it working !!! Yes as.numeric is the trick.. Many thanks again for your help and time. Regards Meenu On Sun, Aug 2, 2009 at 6:06 AM, David Winsemius dwinsem...@comcast.netwrote: Apologies to list: Should have replied to all. -- DW Begin forwarded message: From: David Winsemius dwinsem...@comcast.net Date: August 1, 2009 3:02:58 PM EDT To: Meenu Sahi meenus...@gmail.com Subject: Re: [R] Add columns in a dataframe and fill them from another table according to a criteria On Aug 1, 2009, at 1:43 PM, Meenu Sahi wrote: Dear R users My apologizes for not writing in the correct format due to my ignorance. In the future I will write more clearly. I hope to contribute to the R community in the process of picking up the language professionally. I have now written the R code which is attached in a notepad file. I've simplified my problem in an example of, table pstate which contains the probabilities of getting certain changes in the four different states and a dataframe mydata4 which contains all the changes connected to the four different states. I would like to add the probabilities into mydata4 after matching for the change and the state. Everything before # output can be copy pasted in the R window. The desired output is written after ## OUTPUT Must I write an if else or can I do it in an easier way? Your help is greatly appreciated ! Many thanks for your patience. You need to figure out how to send mail to the list with plain text. But I suspect you did successfully get the attchment through to the audience. I did not like the ordering of the PStates in your new target dataframe so I changed it to fit my(and your) purposes. Change-c(b,a,b,c,d,a) State-c(State1,State4,State2,State3,State1,State3) mydata4-data.frame(Change,State) mydata4-data.frame(mydata4, + PState1=NA, + PState2=NA, + PState3=NA, + PState4=NA + ) mydata4 Change State PState1 PState2 PState3 PState4 1 b State1 NA NA NA NA 2 a State4 NA NA NA NA 3 b State2 NA NA NA NA 4 c State3 NA NA NA NA 5 d State1 NA NA NA NA 6 a State3 NA NA NA NA Note that str(pstate shows that State is a factor which becomes important. This now effects the desired transformation: for (i in 1:length(mydata4) ) { mydata4[i, as.numeric( mydata4[i, State])+2 ] - #assign to the i-th row, State + 2 column in mydata4 ... pstate[ mydata4[i, Change], as.numeric( mydata4[i, State])+1 ] } #... the value of i-th row, State+1 column of pstate mydata4 Change State PState1 PState2 PState3 PState4 1 b State1 Pb1NANANA 2 a State4NANANA Pa4 3 b State2NA Pb2NANA 4 c State3NANA Pc3NA 5 d State1 Pd1NANANA 6 a State3NANA Pa3NA The main non-obvious trick is the as.numeric( mydata4[i, State]) bit. as.numeric() when applied to a factor results in a numeric offset derived from the factor coding rather than using the level names. I suppose I could have left the PStaten's in the original order but then I would have been subtracting them from 7 to get the proper column number. Seemed even less understandable Regards Meenu On Sat, Aug 1, 2009 at 9:43 PM, David Winsemius dwinsem...@comcast.net wrote: On Aug 1, 2009, at 9:52 AM, Meenu Sahi wrote: Deare R users I am new to R. What I want to do is explained below;- I have table called States.Prob which is given below:- This table gives the probabilities of the changes in the swap curve depending on the state of the swap curve. I want to put these probabilities in my dataframe mydata(given after the prob table). Prob of States Changes State1 State2 State3 State4 a Pa1 Pa2 Pa3 Pa4 b Pb1 Pb2 Pb3 Pb4 c Pc1 Pc2 Pc3 Pc4 d Pd1 Pd2 Pd3 Pd4 and I have a dataframe(with 93 rows) called mydata part of which(6 rows) is given below where I want to fill in the last four columns with probabilities taken from States.Prob according to the change and state in mydata4:- Change State PState1 PState2 PState3 PState4 1 b State1 Pb1 2 a State4 Pa4 3 b State2Pb2 4 c State3 Pc3 5 d State1 Pd1 6 a State3 Pa3 What I want to do is highlighted in Red. How can I do this easily
[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 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[spread1.91 spread= Inf Level=-Inf Level=3.24,]) state4-data.frame(mydat3[spread=-Inf spread= -0.02 Level3.24 Level=4.51,]) state5-data.frame(mydat3[spread-0.02 spread= 1.91 Level3.24 Level=4.51,]) state6-data.frame(mydat3[spread1.91 spread= Inf Level3.24 Level=4.51,]) state7-data.frame(mydat3[spread=-Inf spread= -0.02 Level4.51 Level=Inf,]) state8-data.frame(mydat3[spread-0.02 spread= 1.91 Level4.51 Level=Inf,]) state9-data.frame(mydat3[spread1.91 spread= Inf Level4.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:1){ 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 } __ R-help@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.
[R] Add columns in a dataframe and fill them from another table according to a criteria
Deare R users I am new to R. What I want to do is explained below;- I have table called States.Prob which is given below:- This table gives the probabilities of the changes in the swap curve depending on the state of the swap curve. I want to put these probabilities in my dataframe mydata(given after the prob table). Prob of States Changes State1 State2 State3 State4 a Pa1 Pa2 Pa3 Pa4 b Pb1 Pb2 Pb3 Pb4 c Pc1 Pc2 Pc3 Pc4 d Pd1 Pd2 Pd3 Pd4 and I have a dataframe(with 93 rows) called mydata part of which(6 rows) is given below where I want to fill in the last four columns with probabilities taken from States.Prob according to the change and state in mydata4:- Change State PState1 PState2 PState3 PState4 1 b State1 Pb1 2 a State4 Pa4 3 b State2Pb2 4 c State3 Pc3 5 d State1 Pd1 6 a State3 Pa3 What I want to do is highlighted in Red. How can I do this easily? Many thanks for your time. kind regards Meenu P.S. Thanks for your reply John. I've tried to put only the relevant columns of the dataframe. Hope its more clear now. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Add columns in a dataframe and fill them from another table according to a criteria
Dear R users My apologizes for not writing in the correct format due to my ignorance. In the future I will write more clearly. I hope to contribute to the R community in the process of picking up the language professionally. I have now written the R code which is attached in a notepad file. I've simplified my problem in an example of, table pstate which contains the probabilities of getting certain changes in the four different states and a dataframe mydata4 which contains all the changes connected to the four different states. I would like to add the probabilities into mydata4 after matching for the change and the state. Everything before # output can be copy pasted in the R window. The desired output is written after ## OUTPUT Must I write an if else or can I do it in an easier way? Your help is greatly appreciated ! Many thanks for your patience. Regards Meenu On Sat, Aug 1, 2009 at 9:43 PM, David Winsemius dwinsem...@comcast.netwrote: On Aug 1, 2009, at 9:52 AM, Meenu Sahi wrote: Deare R users I am new to R. What I want to do is explained below;- I have table called States.Prob which is given below:- This table gives the probabilities of the changes in the swap curve depending on the state of the swap curve. I want to put these probabilities in my dataframe mydata(given after the prob table). Prob of States Changes State1 State2 State3 State4 a Pa1 Pa2 Pa3 Pa4 b Pb1 Pb2 Pb3 Pb4 c Pc1 Pc2 Pc3 Pc4 d Pd1 Pd2 Pd3 Pd4 and I have a dataframe(with 93 rows) called mydata part of which(6 rows) is given below where I want to fill in the last four columns with probabilities taken from States.Prob according to the change and state in mydata4:- Change State PState1 PState2 PState3 PState4 1 b State1 Pb1 2 a State4 Pa4 3 b State2Pb2 4 c State3 Pc3 5 d State1 Pd1 6 a State3 Pa3 What I want to do is highlighted in Red. How can I do this easily? You may have seen it in red, but we don't, and I, at least, cannot figure out what you intend. (Per the Posting Guide, which you have obviously not yet read, you need to compose your question in plain old monochromatic text and change your mail client so it posts in plain text.) If looking at the help pages for stack() and reshape() does not offer useful information and worked examples that meet your needs then: An approach that would make you more populat in these parts would be to make a simpler example, composed in syntactically correct R, that is complete in itself, and can pasted into an R session. Indicate what you intend as output from this simpler input. Perhaps pstate - read.table(textConnection(Changes State1 State2 State3 State4 + a Pa1 Pa2 Pa3 Pa4 + b Pb1 Pb2 Pb3 Pb4 + c Pc1 Pc2 Pc3 Pc4 + d Pd1 Pd2 Pd3 Pd4), header=TRUE, as.is=TRUE) ?stack data.frame(Change=pstate[,1], prstate =stack(pstate[2:5])$values, state=stack(pstate[2:5])$ind ) #first column is only 4 elements long, but will get recycled # second retreives the probabilities and may need to have as.numeric( ) wrapped around it if they really are numeric. # third returns what started out as column names. Change prstate state 1 a Pa1 State1 2 b Pb1 State1 3 c Pc1 State1 4 d Pd1 State1 5 a Pa2 State2 6 b Pb2 State2 7 c Pc2 State2 8 d Pd2 State2 9 a Pa3 State3 10 b Pb3 State3 11 c Pc3 State3 12 d Pd3 State3 13 a Pa4 State4 14 b Pb4 State4 15 c Pc4 State4 16 d Pd4 State4 Many thanks for your time. kind regards Meenu P.S. Thanks for your reply John. I've tried to put only the relevant columns of the dataframe. Hope its more clear now. \\// [[alternative HTML version deleted]] ^^Note: ^^ David Winsemius, MD Heritage Laboratories West Hartford, CT pstate-read.table(textConnection(Changes PState1 PState2 PState3 PState4 + a Pa1 Pa2 Pa3 Pa4 + b Pb1 Pb2 Pb3 Pb4 + c Pc1 Pc2 Pc3 Pc4 + d Pd1 Pd2 Pd3 Pd4),header=TRUE,as.is=TRUE) Change-c(b,a,b,c,d,a) State-c(State1,State4,State2,State3,State1,State3) mydata4-data.frame(Change,State) mydata4-within(mydata4, { PState1-NA PState2-NA PState3-NA PState4-NA }) #OUTPUT #I would like to see my output of mydata4 with NA in the last 4 columns replaced by matching probabilities # from table pstate in whichever of the 4 columns are applicable depending on the State and Change. e.g. Row1 # of mydata4 has
[R] Fill dataframe from a table according to a criteria
Deare R users I am new to R. What I want to do is explained below;- I have table called States.Prob which is given below Prob of States Changes State1 State2 State3 State4 A Pa1 Pa2 Pa3 Pa4 B Pb1 Pb2 Pb3 Pb4 C Pc1 Pc2 Pc3 Pc4 D Pd1 Pd2 Pd3 Pd4 and I have a dataframe called mydata which is given below where I want to fill in the last four columns of probabilities taken from States.Prob according to the change and state in mydata4:- PxMid EU0006MIndex.x DMSW1Curncy.x DMSW2Curncy.x DMSW3Curncy.x 1 01/01/20032.801132.70702.89203.1720 2 01/01/20073.853504.07604.12504.1230 3 01/02/20023.415633.63854.08104.3587 4 01/02/20062.72.95453.16723.2792 5 01/05/20023.498633.79404.23004.4920 6 01/05/20063.035883.32543.59503.7380 DMSW4Curncy.x DMSW5Curncy.x DMSW6Curncy.x DMSW7Curncy.x DMSW8Curncy.x 13.43603.66403.86404.03604.1790 24.12304.12504.13504.14604.1620 34.54874.69884.83134.93385.0138 43.36273.43423.48993.54373.5941 54.76504.86755.05505.16005.2400 63.82903.90503.96904.02404.0750 Change State PState1 PState2 PState3 PState4 1 B State1 Pb1 2 A State4 Pa4 3 B State2Pb2 4 C State3 Pc3 5 D State1 Pd1 6 A State3 Pa3 What I want to do is highlighted in Red. How can I do this easily? Many thanks for your time. kind regards Meenu [[alternative HTML version deleted]] __ R-help@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.
[R] Sort a dataframe on the column Date
Dear Users I have a dataframe called mydata4 of the following order with the first column as a date and the rest of the columns are numeric with rate. Column 1 Rate1 : Rate 20 (PxMid) 01/01/2003 07/01/2001 -- I wish to sort this dataframe on the first col in ascending order. I tried to do the following mydata4-mydata4[,order(mydata4$PxMid)] This give an error. Please help. Regards Meenu [[alternative HTML version deleted]] __ R-help@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.
[R] Sort a column in a dataframe
Dear Users This is my dataset called mydata4. I want to sort the dataframe on the first column PxMid which is basically a column with dates. I've tried mydata4-mydata4[order(mydata4$PxMid),] but it doesnt work. Could it be because these are dates? Please help I'm really stuck !! Thank you for your time. Regards Meenu PxMid EU0006MIndex.x DMSW1Curncy.x DMSW2Curncy.x DMSW3Curncy.x 1 01/01/20032.801132.70702.89203.1720 2 01/01/20073.853504.07604.12504.1230 3 01/02/20023.415633.63854.08104.3587 4 01/02/20062.72.95453.16723.2792 5 01/05/20023.498633.79404.23004.4920 6 01/05/20063.035883.32543.59503.7380 DMSW4Curncy.x DMSW5Curncy.x DMSW6Curncy.x DMSW7Curncy.x DMSW8Curncy.x 13.43603.66403.86404.03604.1790 24.12304.12504.13504.14604.1620 34.54874.69884.83134.93385.0138 43.36273.43423.48993.54373.5941 54.76504.86755.05505.16005.2400 63.82903.90503.96904.02404.0750 DMSW9Curncy.x DMSW10Curncy.x DMSW12Curncy.x DMSW15Curncy.x DMSW20Curncy.x 14.2990 4.4000 4.5575 4.7175 4.8600 24.1800 4.2000 4.2330 4.2750 4.3120 35.0788 5.1263 5.2100 5.2887 5.3562 43.6457 3.6913 3.7725 3.8669 3.9410 55.3050 5.3575 5.4450 5.5425 5.6300 64.1230 4.1650 4.2360 4.3170 4.3960 DMSW30Curncy.x EUSA40Curncy.x EUSA50Curncy.x spread.x level.x State1 State2 1 4.8925 4.8400 4.7975 2.0005 4.006772 NA NA 2 4.2890 4.2460 4.2040 0.1640 4.165147 NA NA 3 5.3412 5.2913 5.2638 1.2602 4.792737 NA NA 4 3.9840 3.9760 3.9560 0.8168 3.550524 NA NA 5 5.6275 5.5775 5.5375 1.3975 5.007331 NA NA 6 4.4290 4.4190 4.3980 0.8340 3.998781 NA NA State3 State4 State5 State6 State7 State8 State9 1 NA NA NA 6 NA NA NA 2 NA NA 5 NA NA NA NA 3 NA NA NA NA NA NA 9 4 NA NA NA 6 NA NA NA 5 NA NA NA NA NA NA 9 6 NA NA NA 6 NA NA NA [[alternative HTML version deleted]] __ R-help@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.
[R] Connection to SQL Server
Dear Users I am new to R. I'm trying to make a connection to Microsoft SQL Server via R. How can I find the correct syntax to make the connection? The Help pdf file has an example for mySql but there's isnt much on SQL Server. Please help. Thanks ! Me. [[alternative HTML version deleted]] __ R-help@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.
[R] Connection to Microsoft SQL Server
Dear Users I am new to R. I'm trying to make a connection to Microsoft SQL Server via R. How can I find the correct syntax to make the connection? The Help pdf file has an example for mySql but there's isnt much on SQL Server. Please help. I am trying the following code library(RODBC) con-odbcConnect(dbname,hostname) How do I specify the driver,userid,password etc? Many thanks for your help! Me. [[alternative HTML version deleted]] __ R-help@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.