Could not find function "train"


Just first off, ideally, pose your questions with a reproducible example in future (FAQ: What's a reproducible example (`reprex`) and how do I create one?). That makes it much easier to understand the issue and help resolve.

You need to load a package (with the library(PACKNAME) command) before you have access to the data and function contain within. Looks like you're using caret package's train, so you'd want to run,

myModel <- train(...)
1 Like

I'm trying to applicaticate KNN like this example .

when i tried

Problem in the last capture :

As a note, it is extremely hard to help debug code and/or error messages when screenshots are posted. Posting screenshots, instead of actual code/error messages, is likely to decrease your chances of getting the help you are looking for in general. Instead of posting a screenshot, please copy and paste the code and error message and format the pasted code/error messages as shown below.

Put code that is inline (such as a function name, like mutate or filter ) inside of backticks ( mutate ) and chunks of code (including error messages and code copied from the console) can be put between sets of three backticks:

example <- foo %>%
  filter(a == 1)

This process can be done automatically by highlighting your code, either inline or in a chunk, and clicking the </> button on the toolbar of the reply window!

This will help keep our community tidy and help you get the help you are looking for!

For more information, please take a look at the community's FAQ on formating code

Furthermore, your errors seem to relate to your data, which we do not have.
but following the reprex guide you could provide an example of a part.
If you have used read.table to put your data into projet
you can share with us via


and create your example around that.

See the reprex guide for the full version of this sort of forum advice.


Hello, sorry i'm new in this platform.
So, i'm trying to execute this code in my work .

My code :

projet <- read.table(file.choose(), header = TRUE, sep = ",")
complexite <- as.factor(projet$complexite)
Le chargement a nécessité le package : lattice
Le chargement a nécessité le package : ggplot2

knnFit <- train(complexite~., data = Trainingcomplexité,method = "knn", tuneLength = 15,)
Error in model.frame.default(form = complexite ~ ., data = Trainingcomplexité, :
'data' must be a data.frame, environment, or list

NB : This is my data i can't upload this .csv here

I wouldnt ask you to upload your csv, and im not going to try to type it out from your screenshot.
Is there a areason you prefer not to use dput() like I suggested ?

(Is there a areason you prefer not to use dput() like I suggested ?)

I can't understand your question

You give the impression that you don't read our posts when we give you advice...
Perhaps there is a language barrier, and I can understand and be patient with that, but it would be best if you were specific about what you dont understand from what we are saying... I have written extensively, and shared with you link to a beginners guide that I expected would be straightforward to understand and follow , if the reader gives it time.

dput() is a function available in the R language, whose purpose is to represent objects such as data.frame's in a textual i.e. copy and pasteable - appropriate for forums , representation.

1 Like

thank you nirgrahamuk :heart:,
sorry, i will read the guide later .
I used dput() in the train function but same error .

Sorry. You have not at all understood dput or its use. Read the guide. See how the function is discussed in the guide. Come back after that.

Error suggests that your training data is not the right type.

Please execute this command class(Trainingcomplexité). The result should be a data frame.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.