[R] Running a function through a vector with the scalars as arguments

Bill.Venables at csiro.au Bill.Venables at csiro.au
Thu Jun 18 07:08:31 CEST 2009


Why not push it a bit?

theProbs <- runif(10000,0,1)
youreON <- rbinom(length(theProbs), 1, theProbs)  ## note difference


________________________________________
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Henrique Dallazuanna [wwwhsd at gmail.com]
Sent: 18 June 2009 10:17
To: Economics Guy
Cc: r-help at r-project.org
Subject: Re: [R] Running a function through a vector with the scalars as        arguments

Try this:

sapply(theProbs, rbinom, size = 1, n = 1)

You are using probsFoo in lapply, I don't know what is this.

On Wed, Jun 17, 2009 at 9:09 PM, Economics Guy <economics.guy at gmail.com>wrote:

> I have a vector of probabilities that I would like to feed to rbinom()
> and have it return a 1 or 0 for each element. I have tried simply
> using the entire vector as the argument hoping R would understand.
> Instead I get a scalar:
>
>
> theProbs <- runif(10,0,1)
> youreON <- rbinom(1, 1, theProbs)
>
>
>
> Next I tried using the apply function but I could not get that to work
> either:
>
>
> youreON2 <- lapply(probsFoo,rbinom, n=1, size=1, prob=theProbs))
>
>
> I normally would just write a loop to run down the vector and spit out
> the new vector but I am trying to learn to live without loops. Is
> there any way to make this work with some flavor of apply?
>
> ______________________________________________
> 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.
>



--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]




More information about the R-help mailing list