简体   繁体   中英

unused argument in function in R

I'm new to R and trying to use the acceptance rejection method to generate a standard normal variable as part of an an assignment and I can't figure out where I'm off in defining this function. I've looked through other cases with the unused argument error, but they seem to be related to having extra variables as input, which I don't believe I do. Can someone explain why this happens? I would really like to understand better how R works.

AR<-function(u1,u2){
    y1=(-1)*(log(u1))
    y2=(-1)*(log(u2))
    condition=(((y1)-1)*((y1)-1))/2
    u3=runif(1)
    ifelse(u3>=0.5,zz=abs(z),zz=(-1)*(abs(z)))
}

u1=runif(1)
u2=runif(1)
AR(u1,u2)
# Error in ifelse(u3 >= 0.5, zz = abs(z), zz = (-1) * (abs(z))) : 
#  unused argument(s) (zz = abs(z), zz = (-1) * (abs(z)))

ifelse has no arguments called zz (the names are test, yes, no , see ?ifelse ). That's why you get the error unused arguments . The correct usage of ifelse would be:

zz <- ifelse(u3 >= 0.5, abs(z), (-1) * abs(z))

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