As you’re learning R (you *have* read the post about that, right?) you’ll inevitably make some mistakes. That’s normal when learning any language, and it’s especially common when you already know another programming language.
I’m creating this post because this is the first thing you’ll run into – let’s get some good habits going right from the start.
Functions aren’t Commands
A function in R is a command that takes inputs. That means they have a set of parenthesis after the name of the function. For instance, it’s common to type this:
summary
When you mean this:
summary()
Normally R just yells at you and gives you a nasty error message, something like:
function(object, …)
But sometimes typing just the command is dangerous, if you perhaps have set a variable name to that function (a bad idea, by the way) and you’ll actually get a return value!
Fix:
Memorize the () thing. It’s just muscle-memory.
Function names as Variable names
Let’s try that last mistake again – we’ll set a variable name to the name of a function:
summary <-2
That works! Note that in many languages this gives an error. But R will happily let you do it.
Now we forget the parenthesis again when we think we’re calling a function:
summary
And we get an answer. Bad juju.
Fix:
Check the main functions here: http://www.statmethods.net/management/functions.html
Also, when you load packages, they will have functions as well. Check those, too.
And use a GUI at first. Most of them color-code functions, just like other IDE’s.
R is sensitive (but not in a PC way)
Another mistake I see a lot in beginning R students is forgetting that R cares about case. In other words, the variable “a” is a separate thing than the variable “A”.
NOTE: Package names can be case-sensitive as well.
Fix:
Remember: It’s a UNIX kind of software thing. Those are sensitive like that. Develop a standard (use the ones from statistics, to start) and stick with it. Also, this is a good video about variables: https://www.youtube.com/watch?v=weZzE2_cyj4
Giving a little too much Space
This is another syntax thing – but one with a bite. Try it – we’ll assign 10 to the variable x:
x<-10
x
We get back what we expect, 10. But add a simple space:
x < - 10
x
..and you get a different answer entirely, FALSE or perhaps even TRUE. Youch.
Fix:
Use an IDE or GUI to start. It will help you with color-coding. Also, run small batches of your code first, to ensure you get the answers you expected.
El código no es código (Your code is not my code)
You get some code from the web to try a thing and it won’t run. It fails with a
Error: could not find function
error.
Fix:
You’re missing a package. You’ll need to go read more where you got that code, and find out which packages it requires. Find out what libraries you have loaded:
library()
You can try this, subbing in the name of the function that it failed on:
require(functioname)
Note that the function name is not always the name of the package! A package often contains many functions, but a quick web search will soon locate what you need. Watch the versions too.
If you don’t have the right package, use the
install.packages()
function to load the one you need.
OK – that’s enough to get started with for now. As we run into more issues, I’ll post more of these. You can also find many on the web with a quick search – this is a great resource, although it can be a bit advanced: http://www.burns-stat.com/pages/Tutor/R_inferno.pdf
great, now I want steak…
LikeLiked by 1 person