. Advertisement .
..3..
. Advertisement .
..4..
The error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator appears when I run my program:
out.liar <- function(variable) { # arguments can be called whatever
remove <- boxplot(variable, plot = F)$out
variable[variable %in% remove] <- NA
return(variable)
}
When I pass a character vector to the out.liar()
function, it throws an error.
out.liar(letters)
## Error in x[floor(d)] + x[ceiling(d)]: non-numeric argument to binary operator
Please tell me the cause and how to fix the above error.
The cause: The error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator happened was that the letter vector was sent from
out.liar()
toboxplot()
, which then passed it on to the+
function. The code failed because you cannot add letters, which brokeboxplot()
andout.liar()
.Solution: You could also want to add the ability to locate outliers in a data frame to the function. To accomplish that, you must first make the function recognize which columns in the provided data frame are numerical before executing the code on each of these columns. The function should then return the full data frame, with the numeric columns updated and the non-numeric columns.
Check to see whether it functions, make a data frame first:
Followed by feeding
df
toout.liar()
:Finally, verify that the adjustment does not affect the function’s ability to work on single columns: