Hello, I am trying to implement the following formula using for loops and vectors. I am sure you can use some fancy R code to solve this but I would like to keep it simple and stick to for and vector/array if that is possible. TP = 200; RL = 50; TPR1 = TP - RL; TPR2 = TP + RL; PPO = 0; LSS = 0.1; counter = 1; for(i in res) { # Even if(counter %% 2 == 1) { ls = abs((sum(LSS)* TP)) / (TPR1 - PPO); LSS = c(LSS,ls); } # Odd if(counter %% 2 == 0) { ls = abs((sum(LSS)* TPR2)) / (TP - PPO); LSS = c(LSS,ls); } } -------------- next part -------------- A non-text attachment was scrubbed... Name: Unbenannt.PNG Type: image/png Size: 146426 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20170620/29699747/attachment.png>
This is an excellent exercise for you, the beginner. If you explicitly want a line-by-line translation and don't want to use the strengths of R (vectorization/functions) then there isn't much point in asking us to read the Introduction to R document that comes with the software for you. Please read the Posting Guide before posting again... your failure to switch your email client to plain text is going to lead to corruption of your question by the time w we see it eventually. PS you did not define what "res" is... -- Sent from my phone. Please excuse my brevity. On June 20, 2017 12:44:13 AM PDT, Wolfgang K <mails00000 at gmail.com> wrote:>Hello, > >I am trying to implement the following formula using for loops and >vectors. >I am sure you can use some fancy R code to solve this but I would like >to >keep it simple and stick to for and vector/array if that is possible. > >TP = 200; >RL = 50; >TPR1 = TP - RL; >TPR2 = TP + RL; >PPO = 0; > >LSS = 0.1; > >counter = 1; > >for(i in res) { > > # Even > if(counter %% 2 == 1) { > ls = abs((sum(LSS)* TP)) / (TPR1 - PPO); > LSS = c(LSS,ls); > > } > > # Odd > if(counter %% 2 == 0) { > > ls = abs((sum(LSS)* TPR2)) / (TP - PPO); > LSS = c(LSS,ls); > > } >}
Cc'd back to the list... always use reply-all. You say res should have been LSS but LSS is a scalar so the for loop will only run once. What does a successful output look like for a sample input? How do you (we) know when success has been achieved? In fact, what is the formula you want to implement? If your code below is the definition of your "formula" then we are left with no destination. You either need to add a reference (paper citation?) or a set of inputs and corresponding outputs. Here are some resources that describe how to make a reproducible example [1][2][3]: [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example [2] http://adv-r.had.co.nz/Reproducibility.html [3] https://cran.r-project.org/web/packages/reprex/index.html -- Sent from my phone. Please excuse my brevity. On June 20, 2017 7:01:23 AM PDT, Wolfgang K <mails00000 at gmail.com> wrote:>Hi, > >well, I know how to use for-loop and create vectors. That is not the >point. However, implementing this code is - at least for me - not as >trivial as it seems since you have changes in the signs as well as >changes in multiplication by TPR1 and TPR2. I spent hours trying to >get this peace of code sorted out but I just don't get it working. I >managed to calculate the first two values L2 and L3 but no more. Since >I am not advanced in R, I was asking for help with basic functions >which I do know already. > >Yes I changed so much that I forgot to change res back to LSS. > >2017-06-20 15:46 GMT+02:00 Jeff Newmiller <jdnewmil at dcn.davis.ca.us>: >> This is an excellent exercise for you, the beginner. If you >explicitly want a line-by-line translation and don't want to use the >strengths of R (vectorization/functions) then there isn't much point in >asking us to read the Introduction to R document that comes with the >software for you. >> >> Please read the Posting Guide before posting again... your failure to >switch your email client to plain text is going to lead to corruption >of your question by the time w we see it eventually. >> >> PS you did not define what "res" is... >> -- >> Sent from my phone. Please excuse my brevity. >> >> On June 20, 2017 12:44:13 AM PDT, Wolfgang K <mails00000 at gmail.com> >wrote: >>>Hello, >>> >>>I am trying to implement the following formula using for loops and >>>vectors. >>>I am sure you can use some fancy R code to solve this but I would >like >>>to >>>keep it simple and stick to for and vector/array if that is possible. >>> >>>TP = 200; >>>RL = 50; >>>TPR1 = TP - RL; >>>TPR2 = TP + RL; >>>PPO = 0; >>> >>>LSS = 0.1; >>> >>>counter = 1; >>> >>>for(i in res) { >>> >>> # Even >>> if(counter %% 2 == 1) { >>> ls = abs((sum(LSS)* TP)) / (TPR1 - PPO); >>> LSS = c(LSS,ls); >>> >>> } >>> >>> # Odd >>> if(counter %% 2 == 0) { >>> >>> ls = abs((sum(LSS)* TPR2)) / (TP - PPO); >>> LSS = c(LSS,ls); >>> >>> } >>>}
The attached png file shows the expected output. The loop is complicated because the values multiplied shift in value and sign so your effort to sum the values along the way fails. It we compute the first two values directly and create arrays to handle the changing multipliers, the loop is pretty simple:> TP <- 200 > RL <- 50 > TPR1 <- TP - RL > TPR2 <- TP + RL > PPO <- 0 > LSS <- 0.1 > res <- 3:10 > L <- array(0, 10) > L[1] <- .1 > L[2] <- abs(L[1] * TP) / (TPR1 - PPO) > rows <- rbind(rep(c(-TP, TPR1), 5), rep(c(TP, -TPR2), 5)) > rows # These are the alternating values that are multiplied by L[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] -200 150 -200 150 -200 150 -200 150 -200 150 [2,] 200 -250 200 -250 200 -250 200 -250 200 -250> dnom <- c(TPR1 - PPO, TP - PPO) > dnom # These are the alternating denominators[1] 150 200> > for (i in res) {+ r <- i %% 2 + 1 + s <- seq_len(i-1) + L[i] <- abs(sum(L[s] * rows[r, s]))/ dnom[r] + }> L[1] 0.10000000 0.13333333 0.06666667 0.08888889 0.11111111 0.14814815 0.18518519 [8] 0.24691358 0.30864198 0.41152263 Matches the values in your png. ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff Newmiller Sent: Tuesday, June 20, 2017 9:38 AM To: Wolfgang K <mails00000 at gmail.com> Cc: R-help <r-help at r-project.org> Subject: Re: [R] translate formula into R code Cc'd back to the list... always use reply-all. You say res should have been LSS but LSS is a scalar so the for loop will only run once. What does a successful output look like for a sample input? How do you (we) know when success has been achieved? In fact, what is the formula you want to implement? If your code below is the definition of your "formula" then we are left with no destination. You either need to add a reference (paper citation?) or a set of inputs and corresponding outputs. Here are some resources that describe how to make a reproducible example [1][2][3]: [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example [2] http://adv-r.had.co.nz/Reproducibility.html [3] https://cran.r-project.org/web/packages/reprex/index.html -- Sent from my phone. Please excuse my brevity. On June 20, 2017 7:01:23 AM PDT, Wolfgang K <mails00000 at gmail.com> wrote:>Hi, > >well, I know how to use for-loop and create vectors. That is not the >point. However, implementing this code is - at least for me - not as >trivial as it seems since you have changes in the signs as well as >changes in multiplication by TPR1 and TPR2. I spent hours trying to >get this peace of code sorted out but I just don't get it working. I >managed to calculate the first two values L2 and L3 but no more. Since >I am not advanced in R, I was asking for help with basic functions >which I do know already. > >Yes I changed so much that I forgot to change res back to LSS. > >2017-06-20 15:46 GMT+02:00 Jeff Newmiller <jdnewmil at dcn.davis.ca.us>: >> This is an excellent exercise for you, the beginner. If you >explicitly want a line-by-line translation and don't want to use the >strengths of R (vectorization/functions) then there isn't much point in >asking us to read the Introduction to R document that comes with the >software for you. >> >> Please read the Posting Guide before posting again... your failure to >switch your email client to plain text is going to lead to corruption >of your question by the time w we see it eventually. >> >> PS you did not define what "res" is... >> -- >> Sent from my phone. Please excuse my brevity. >> >> On June 20, 2017 12:44:13 AM PDT, Wolfgang K <mails00000 at gmail.com> >wrote: >>>Hello, >>> >>>I am trying to implement the following formula using for loops and >>>vectors. >>>I am sure you can use some fancy R code to solve this but I would >like >>>to >>>keep it simple and stick to for and vector/array if that is possible. >>> >>>TP = 200; >>>RL = 50; >>>TPR1 = TP - RL; >>>TPR2 = TP + RL; >>>PPO = 0; >>> >>>LSS = 0.1; >>> >>>counter = 1; >>> >>>for(i in res) { >>> >>> # Even >>> if(counter %% 2 == 1) { >>> ls = abs((sum(LSS)* TP)) / (TPR1 - PPO); >>> LSS = c(LSS,ls); >>> >>> } >>> >>> # Odd >>> if(counter %% 2 == 0) { >>> >>> ls = abs((sum(LSS)* TPR2)) / (TP - PPO); >>> LSS = c(LSS,ls); >>> >>> } >>>}______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.