I have an array of probabilities....it is p. So if user types x=1 then probability is p1=1/10. If user types x=2 it means that p2= p1+p2 if user types x=3 it means that p3=p1+p2+p3....and so on. So i created a code..... but it doesnt work properly. Help me plz to fix it) Thank u in advance. psidp=function(x){ p<-(1/10 2/5 2/5 2/5 2/5 1/10 1/10 1/10 1/10 1/10) i==0; if (x!=0){ for (i in 1 to x) {p[i]=p[i]+p[i-1] p[i] } } -- View this message in context: http://r.789695.n4.nabble.com/Help-plz-to-fix-it-tp4647051.html Sent from the R help mailing list archive at Nabble.com.
I think this is what you want. p <- c(1/10, 2/5, 2/5, 2/5, 2/5, 1/10, 1/10, 1/10, 1/10, 1/10) psidp <- function(x){ if (x>0&&x<=10) { return(sum(p[1:x])) } else{ return("Input integer between 1 and 10") }} -- View this message in context: http://r.789695.n4.nabble.com/Help-plz-to-fix-it-tp4647051p4647063.html Sent from the R help mailing list archive at Nabble.com.
This looks like homework to me. Here's a hint, though: p<-(1/10 2/5 2/5 2/5 2/5 1/10 1/10 1/10 1/10 1/10) This is not how you create a vector in R. for (i in 1 to x) This is not how you create a for loop in R. Sarah On Mon, Oct 22, 2012 at 2:41 PM, Rlotus <yerlan_ at hotmail.com> wrote:> I have an array of probabilities....it is p. So if user types x=1 then > probability is p1=1/10. > If user types x=2 it means that p2= p1+p2 > if user types x=3 it means that p3=p1+p2+p3....and so on. > So i created a code..... but it doesnt work properly. Help me plz to fix it) > Thank u in advance. > > psidp=function(x){ > p<-(1/10 2/5 2/5 2/5 2/5 1/10 1/10 1/10 1/10 1/10) > i==0; > if (x!=0){ > for (i in 1 to x) > {p[i]=p[i]+p[i-1] > p[i] > } > > } > > >-- Sarah Goslee http://www.functionaldiversity.org
Hello, Try ?cumsum. cumsum(p) Hope this helps, Rui Barradas Em 22-10-2012 19:41, Rlotus escreveu:> I have an array of probabilities....it is p. So if user types x=1 then > probability is p1=1/10. > If user types x=2 it means that p2= p1+p2 > if user types x=3 it means that p3=p1+p2+p3....and so on. > So i created a code..... but it doesnt work properly. Help me plz to fix it) > Thank u in advance. > > psidp=function(x){ > p<-(1/10 2/5 2/5 2/5 2/5 1/10 1/10 1/10 1/10 1/10) > i==0; > if (x!=0){ > for (i in 1 to x) > {p[i]=p[i]+p[i-1] > p[i] > } > > } > > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-plz-to-fix-it-tp4647051.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Hi, May be this helps: psidp<-function(x){ ?p <- c(1/10, 2/5, 2/5, 2/5, 2/5, 1/10, 1/10, 1/10, 1/10, 1/10) ?ifelse(x>0 & x<=length(p),sum(p[seq(1,x,1)]),NA)} psidp(0) #[1] NA ?psidp(5) #[1] 1.7 ?psidp(10) #[1] 2.2 A.K. ----- Original Message ----- From: Rlotus <yerlan_ at hotmail.com> To: r-help at r-project.org Cc: Sent: Monday, October 22, 2012 2:41 PM Subject: [R] Help plz to fix it I have an array of probabilities....it is p. So if user types x=1 then probability is p1=1/10. If user types x=2 it means that p2= p1+p2 if user types x=3 it means that p3=p1+p2+p3....and so on. So i created a code..... but it doesnt work properly. Help me plz to fix it) Thank u in advance. psidp=function(x){ ? p<-(1/10 2/5 2/5 2/5 2/5 1/10 1/10 1/10 1/10 1/10) ? i==0; ? if (x!=0){ ? ??? for (i in 1 to x) ? ??? {p[i]=p[i]+p[i-1] ? ??? p[i] ? ??? ??? } } ? ??? ? ??? ? ??? ? ??? ? ??? ??? -- View this message in context: http://r.789695.n4.nabble.com/Help-plz-to-fix-it-tp4647051.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.