简体   繁体   中英

R code with > symbol

Can someone please kindly explain this line of code? I don't quite seem to understand how it works?

    decision <- (a > 0.5) * 1
    a <- 0.3
    decision
    # [1] 1
    a <- 0.001
    decision
    # [1] 1
    a <- 100
    decision
    # [1] 1

if a is 0.3, I get 1.
if a is 0.001, I get 1.
if a is 100, I get 1.

Are you trying to create a function named decision ? What happens in your code is that decision is set at the time of its creation, and never modified after that. You already had set a to some value before running the code that you showed us, and decision was set based on that.

This is how to create a function. It should take a as a parameter, btw.

decision <- function(a) (a > 0.5) * 1
decision(0.3)
# 0
decision(0.001)
# 0
decision(100)
# 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM