Recreate for loop without using for loop

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Recreate for loop without using for loop

rimae95
Hi.



Can someone please help me recreate this code without using a for loop? Idk if I'm supposed to use a map function or something else.



qprob <- function(pp) {

  qq <- 1 - pp -1

  stotal <- 0.0

  for (i in 1:length(pp))

    stotal <- stotal + pp[i] * prod(qq[-i])

  return(stotal)

}

Best regards,
Rima


Sendt fra Mail<https://go.microsoft.com/fwlink/?LinkId=550986> til Windows 10


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Recreate for loop without using for loop

Jeff Newmiller
There is a no-homework policy stated in the Posting Guide.

On February 10, 2019 8:59:41 AM PST, Rima El-zein <[hidden email]> wrote:

>Hi.
>
>
>
>Can someone please help me recreate this code without using a for loop?
>Idk if I'm supposed to use a map function or something else.
>
>
>
>qprob <- function(pp) {
>
>  qq <- 1 - pp -1
>
>  stotal <- 0.0
>
>  for (i in 1:length(pp))
>
>    stotal <- stotal + pp[i] * prod(qq[-i])
>
>  return(stotal)
>
>}
>
>Best regards,
>Rima
>
>
>Sendt fra Mail<https://go.microsoft.com/fwlink/?LinkId=550986> til
>Windows 10
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Recreate for loop without using for loop

Rolf Turner
In reply to this post by rimae95

This list has a "no homework" policy.

On 2/11/19 5:59 AM, Rima El-zein wrote:
> Hi.
>
>
>
> Can someone please help me recreate this code without using a for loop? Idk if I'm supposed to use a map function or something else.

Supposed by whom?  What do you mean by "map function"?

> qprob <- function(pp) {
>
>    qq <- 1 - pp -1
>
>    stotal <- 0.0
>
>    for (i in 1:length(pp))
>
>      stotal <- stotal + pp[i] * prod(qq[-i])
>
>    return(stotal)
>
> }

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.