0

I am trying to test some code in R, but it keeps giving me an error that the tilde key (~) is an unexpected symbol?

Is there something that can be done to fix this?

This was one of the codes I was trying to run:

# kNN
set.seed(123)
fit.knn <-  train(job permanency~., data=dataset, method="knn", metric=metric, preProc=c("permanency rate", "job skills"), trControl=control)

and I get the following error

Error: unexpected symbol in "fit.knn <- train(job permanency"

0

1 Answer 1

8

This actually has nothing to do with the tilde operator. The unexpected symbol is the second word in what is presumed to be a syntactically invalid column name. If job permanency is the name of a column, you will need backticks around it because it is a non-standard name. Try using

train(`job permanency` ~ ., ...)

But in general I would recommend using standard naming conventions (i.e. without spaces). You can convert your current names to syntactically valid names with

names(dataset) <- make.names(names(dataset))

job permanency will become job.permanency, and backticks will no longer be required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.