The error message indicates that one or more of the levels in the class
variable are not valid R variable names. When using R, variable names must start with a letter and contain only letters, numbers, and the dot (.) or underscore (_) characters.
To correct this R-code error, you’ll need to modify the levels of the class
variable so that they are valid R variable names. One solution is to add this code:
# Replace invalid characters in class levels with underscores
levels(training$class) <- gsub("[^[:alnum:]]", "_", levels(training$class))
# Check that the levels are now valid R variable names
isValid <- sapply(levels(training$class), is.validName)
if (any(!isValid)) {
stop("Class levels are still not valid R variable names")
}
Once you have corrected the levels, you should be able to run the rest of the code without encountering the “Error: At least one of the class levels is not a valid R variable name; This will cause errors when class probabilities are generated because the variables names will be converted..”