[R] function runif in for loop
Bert Gunter
gunter.berton at gene.com
Wed Aug 10 20:42:47 CEST 2011
Duncan:
Yup, you're right. Can't assign, just print.
-- Bert
On Wed, Aug 10, 2011 at 11:02 AM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> On 10/08/2011 1:16 PM, Bert Gunter wrote:
>>
>> Duncan et. al:
>>
>> Inline below.
>>
>> On Wed, Aug 10, 2011 at 9:48 AM, Duncan Murdoch
>> <murdoch.duncan at gmail.com> wrote:
>> > On 10/08/2011 9:40 AM, Johannes Radinger wrote:
>> >>
>> >> Jean,
>> >>
>> >> thank you for your answer.
>> >> especially the line X<- numeric(length(lT)) helped me a lot.
>> >>
>> >> Anyway, in my case I'd like to get a "dynamic" variable or better a
>> >> function for X. I mean if i try to call X I'd like that this drawing
>> >> of
>> >> random number is performed. In the case now if I call X several times
>> >> I'll
>> >> always get the same random numbers.
>> >
>> > Such things do exist in R, but they aren't easy to set up.
>>
>> Well, how about:
>>
>> Well... How about:
>>
>> X<- function()runif(1)
>> class(X)<- c("wizz",class(X))
>> print.wizz<- function(x){y<-x(); print(y);y }
>>
>> > X
>> [1] 0.875768
>> > X
>> [1] 0.955208
>> > X
>> [1] 0.1150938
>> > z<- X
>> > z
>> [1] 0.3760085
>> > z<- X
>> > z
>> [1] 0.1506062
>> >
>
> That sort of looks as if it works, but it doesn't:
>
> for (i in 1:3) {
> r <- X + 1
> print(r)
> }
>
> Duncan Murdoch
>
>> Cheers,
>> Bert
>>
>>
>> Why not just
>> > make X be a function explicitly? That is,
>> >
>> > X<- function() runif(length(lT), lT, uT)
>> >
>> > Then use X() to call the function where you were previously using X.
>> >
>> > Duncan Murdoch
>> >>
>> >> I thought about something like:
>> >> X<- for (i in 1:length(lT)) runif(1, lT[i], uT[i])
>> >>
>> >> So that I can use X as a variable for multiple runs and
>> >> each run new random values are used.
>> >>
>> >> thank you
>> >> Johannes
>> >>
>> >> -------- Original-Nachricht --------
>> >> > Datum: Wed, 10 Aug 2011 08:19:07 -0500
>> >> > Von: Jean V Adams<jvadams at usgs.gov>
>> >> > An: "Johannes Radinger"<JRadinger at gmx.at>
>> >> > CC: r-help at r-project.org
>> >> > Betreff: Re: [R] function runif in for loop
>> >>
>> >> > Johannes,
>> >> >
>> >> > You have the loop set up right, you just need to add indexing to
>> >> refer
>> >> > to
>> >> > the looping variable, i.
>> >> >
>> >> > lT<- sample(1:10)
>> >> > uT<- sample(21:30)
>> >> > X<- numeric(length(lT))
>> >> >
>> >> > for (i in 1:length(lT)) X[i]<- runif(1, lT[i], uT[i])
>> >> >
>> >> > X
>> >> >
>> >> > Note that I changed the name of the result from T to X, because T
>> >> has
>> >> > special meaning in R.
>> >> >
>> >> > Jean
>> >> >
>> >> > `·.,,><(((º> `·.,,><(((º> `·.,,><(((º>
>> >> >
>> >> > Jean V. Adams
>> >> > Statistician
>> >> > U.S. Geological Survey
>> >> > Great Lakes Science Center
>> >> > 223 East Steinfest Road
>> >> > Antigo, WI 54409 USA
>> >> >
>> >> >
>> >> >
>> >> > From:
>> >> > "Johannes Radinger"<JRadinger at gmx.at>
>> >> > To:
>> >> > r-help at r-project.org
>> >> > Date:
>> >> > 08/10/2011 07:23 AM
>> >> > Subject:
>> >> > [R] function runif in for loop
>> >> > Sent by:
>> >> > r-help-bounces at r-project.org
>> >> >
>> >> >
>> >> >
>> >> > Hello,
>> >> >
>> >> > I'd like to perform a regression using MCMCregress (MCMCpack).
>> >> > One variable therefore should be a function rather than a
>> >> variable:
>> >> >
>> >> > I want to use X as an input and X should be defined as a random
>> >> number
>> >> > between to values. Therefore I want to use the function runif
>> >> like:
>> >> > X<-(1, Xa, Xb) but it seems that runif doesn't allow to use
>> >> vectors.
>> >> > So I think I've to calculate the new vector X by using a for
>> >> loop.
>> >> >
>> >> > I tried "for (i in 1:length(lT)) T<-runif(1,lT,uT)" but that
>> >> doesn't
>> >> > work.
>> >> > What is the correct for-loop function to create this new
>> >> > vector/variable?
>> >> >
>> >> > Can I use that function then as an input for MCMCregress?
>> >> >
>> >> > thank you
>> >> >
>> >> > Johannes
>> >> >
>> >> > --
>> >> >
>> >> > ______________________________________________
>> >> > 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.
>> >> >
>> >> >
>> >>
>> >> --
>> >>
>> >> ______________________________________________
>> >> 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.
>> >
>> > ______________________________________________
>> > 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.
>> >
>>
>>
>>
>
>
--
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."
-- Maimonides (1135-1204)
Bert Gunter
Genentech Nonclinical Biostatistics
467-7374
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list