Hi @mmarion,
The value "y"
in quotations is a string (or length 1 character vector, but thats besides the point). The object y
without quotations is a numeric vector containing those numbers you assigned to that name. So you should refer to y
without the quotations in this case.
Next, the is.numeric()
function returns a TRUE
or FALSE
logical value. Also, you don't need the added condition of equality in your if()
statements, since the is.numeric()
already return the TRUE
or FALSE
logicals.
Try this:
z <- function(i, y) {
i = i
flag = 0
if(is.numeric(y)) {
mean(y) / 100
flag = 1
flag
}
if(is.integer(y)) {
mean(y) / 100
flag = 1
flag
}
if(flag==0) {
cat("output error message\n")
}
}
Note that this function has some odd habits. Integers are numeric, but not all numerics are integers. Consider some else
statements to clean up the flow, unless this is the desired behaviour. The last object in a function gets returned (unless you call an explicit return()
, so think about what you want this function to return to the user.