[R] R-help

Jeffrey Spies jspies at virginia.edu
Tue Oct 5 08:38:42 CEST 2010


As Joshua said, in your example sim isn't be declared anywhere
(neither in the environment nor as an argument in a function), but you
might try something more R-ish:

prop.doubles <- function(sim){
    sum(sample(1:6, sim,replace=T)==sample(1:6,sim,replace=T))/sim
}
prop.doubles(1000)

Cheers,

Jeff.

On Tue, Oct 5, 2010 at 2:30 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> On Mon, Oct 4, 2010 at 9:13 PM, Lemarian WallaceIII <tott_03 at yahoo.com> wrote:
>> Im trying to simulate the rolling of a pair of dice
>>
>> this is my function:
>> #function to simulate tosses of a pair of dice
>> #from the simulation, the program returns the empirical probability of
>> #observing a double
>> count <- 0
>> for(j in 1:sim){#begin loop
>> die1 <- sample(1:6,1)
>> print(die1)
>> die2 <- sample(1:6,1)
>> print(die2)
>> count <- ifelse(die1 == die2, count + 1, count)
>> }#end loop
>> emprob <- count/sim
>> return(count,emprob)
>> } #end program
>>
>>
>> these are the errors that keep coming up:
>> Error in 1:sim : 'sim' is missing
>
> You need to define an object called 'sim', otherwise you are telling R
> to go look up the value of a non-existent variable.  Does your
> function have a first part?  All I am seeing is the body and the end.
>
> Josh
>
>>
>>
>> How do I correct this?
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> 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.
>>
>>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.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.
>



More information about the R-help mailing list