How to get help when you are stuck? How to find and fix errors? That is the hardest part for beginners. You can start by checking the list of frequently used functions. And, there is always Google… Any question you may have as a beginner has been asked and answered before. Often on Stackoverlfow.
But at first it is hard to find the right search terms, and to distinguish between good answers, and “solutions” that just pull you down further into the hole you are in.
When asking a question about how to do something in R, it is very important to simplify it as much as possible, and focus on the nucleus of the problem only. That is, do not show a long script where all kinds of things happen that are OK. Show a short script that gets you up to the point where you are stuck. Such as script should be reproducible and self-contained.
Reproducible means that anyone can run it in R and get the same
results. So you need to include
set.seed. Self-contained means that
it should not point to files you only have. You could make these
available, but why complicate things? Just create some data with code or
use a data set that comes with R (e.g. “cars”, “iris”, but there are
many). See the examples in the R help files for 1000s of ideas of how
you can do this.