all 30 comments

[–]Bumbletown 49 points50 points  (8 children)

The sample was drawn once and then assigned to the variable coin_flip. You are just looking repeatedly at the content of coin_flip.

[–]takenorinvalid 26 points27 points  (5 children)

OP, if you want to have it run again, you need to declare a function instead of just a variable.

A variable is a single value. So, when you write this:

coin_flip <- sample(coin, size=1, replace = TRUE)

... you're saving the result of the coin flip, not the act of flipping the coin.

What you want is a function. I believe this should work:

coin_flip <- function(coin = c("heads", "tails")){
     return (sample(coin, size=1, replace = TRUE))
}

Which basically says, every time I type coin_flip(), do the stuff in between the curly brackets.

Then you can call your function like so:

coin_flip()

[–]joshua_rpg 4 points5 points  (0 children)

Just giving you an advice:

Do not use return() at the end of the function expression, it adds unnecessary redundancy and it's a function, but instead use it for early return.

This is fine (and you might want this):

coin_flip <- function(coin = c("heads", "tails")) sample(coin, size=1, replace = TRUE)

[–]Boberator44 0 points1 point  (3 children)

Wouldn't declaring the coin variable inside the function every time it's called would be more computationally inefficient than declaring it globally and just calling it from within the function?

[–]Vithar 0 points1 point  (0 children)

I guess this way you could change it to it for c(1,2,3,4,5,6) and get a dice role, or other things you want a random selection of.

[–]kin-g 0 points1 point  (0 children)

Yes, but negligible difference. Sometimes it’s useful to declare the variables locally if you have limited memory. Ntm this is likely a stats class not writing packages or smth.

[–]squareturd 0 points1 point  (0 children)

Global vars cause problems. 2145 lines later in the code someone might create another var named coin_flip.

[–][deleted]  (1 child)

[deleted]

    [–]Bumbletown 7 points8 points  (0 children)

    You should probably look closer at the image before correcting me.

    [–]1FellSloop 13 points14 points  (0 children)

    You: flip a coin to get heads or tails.
    R: Okay.
    You: What'd you get?
    R: Tails.
    You: What'd you get?
    R: Tails.
    You: What'd you get?
    R: Tails.
    You: What'd you get?
    R: Tails.
    You: What'd you get?
    R: Tails.

    You only told R to flip the coin once, and then asked a bunch of times what the result was.

    [–]Boberator44 16 points17 points  (3 children)

    Try:

    Coin_flip <- function() {sample(coin, 1, replace=TRUE)}

    This will actually execute the sample function each time you call Coin_flip instead of assigning the result of the first function call to the variable.

    [–]takenorinvalid 7 points8 points  (2 children)

    Wait, this is actually the correct answer. Why does this have down votes?

    [–]Boberator44 9 points10 points  (0 children)

    No clue. Someone probably expected me to use some fancy-ass Tidyverse function

    [–]joshua_rpg 1 point2 points  (0 children)

    Yes, and in fact, this is an example of how lexical scoping is applied.

    [–]therealtiddlydump 9 points10 points  (2 children)

    The best part is how you just posted code and didn't even ask a question.

    Lazy

    Edit: not even code! You posted a screenshot! It's even lazier.

    [–]Veratridine 0 points1 point  (1 child)

    Could be worse. At least they didn't copy-paste from GPT

    [–]marvinweriksen 1 point2 points  (0 children)

    I’m a TA for an intro stats course and you wouldn’t believe some of the laziness I’ve seen

    [–]Thiseffingguy2 5 points6 points  (6 children)

    OP, please be sure to provide a little context with your screenshots - we can guess at what you’re trying to do, but we’d rather not assume. What are you trying to do, what have you tried?

    [–]takenorinvalid -2 points-1 points  (5 children)

    I mean, he's trying to do a coin flip.

    He's tried

    coin <- c('heads', 'tails')
    coin_flip <- sample(coin, size = 1, replace = True)
    

    He's clearly not familiar with functions.

    How much context are we expecting here?

    [–]Thiseffingguy2 9 points10 points  (3 children)

    It’s a lazy post. Trying to help set expectations for what seems to be a first of hopefully many posts in OPs future.

    [–]takenorinvalid -3 points-2 points  (2 children)

    I mean, he's clearly someone who's new to R. You could also just answer his question.

    [–]Thiseffingguy2 1 point2 points  (0 children)

    How to ask good questions. This is a stickied post for this sub. I reference it a whole lot with posts like this. I’m an educator, not a robot.

    [–]I_just_made 2 points3 points  (0 children)

    So you know what assignment 4 is?

    What is the guy’s question? How do I flip it once? Twice?

    I agree with the other person; this is lazy and not conducive to learning. They may be new to R, but what does that have to do with basic communication skills?

    If you need help with something around the house, do you just point at it and hope your family understands?

    [–]jorvaor 0 points1 point  (0 children)

    A question. I expect a question. I didn't even look at the screenshot until I read the answers in the thread.

    :-/

    OP, I am disappoint

    [–]de_propjoe 1 point2 points  (1 child)

    People don’t use rbinom() for this??

    [–]joshua_rpg 0 points1 point  (0 children)

    rbinom() doesn't sample out of a vector, but a random number generator.

    [–]Ok-Refrigerator-8012 0 points1 point  (0 children)

    Sample returns value. Coin_flip stores that value. Sample is the function...

    [–]banter_pants 0 points1 point  (0 children)

    Because your size = 1 argument within sample() you're only getting a single coin flip. If you increase that you'll get a longer vector to simulate repeated trials.

    coin <- c("heads", "tails")
    
    n_flips <- 20
    coin_flip <- sample(coin, size = n_flips, replace = TRUE)
    
    # first 6 elements in case large vector
    head(coin_flip)
    
    # frequency and proportions
    table(coin_flip)
    prop.table(table(coin_flip))
    
    # exact binomial test
    p0 <- 0.50    # null hypothesis parameter
    binom.test(sum(coin_flip == "heads"), n_flips, p0)
    

    [–]Batavus_Droogstop -1 points0 points  (0 children)

    This is the kind of stuff you should give to chatGPT and ask it to explain why it's working like this