Although coding in R is rather intuitive, no one remembers all the available functions and commands. In fact we do not have to remember anything. We have got quite good built-in help in R and the Internet. In this post I will show you how to use them both to solve common R problems.
The most common R programming problems
- I know the R function but I am not sure how it works.
- I know what I want to do but I don’t know R function that implements it.
- I know R function, I know how it works but it doesn’t work as I expected.
I know the R function but I am not sure how it works.
In this case you should use help function. You can use it in 3 different ways:
- place “?” before the name of the function –
- use it as any other function –
- place the cursor in the name of the function and press F1 button – that is my favourite way because you can use it while typing – you do not need change your active window to console (in RStudio)
If the only information you need is a list of arguments that function takes – use the
If you would like to get information about whole package you need to specify that fact –
# you can't remember how to use rnorm function ?rnorm # this will have the same effect help(rnorm) # list of arguments of rnorm args(rnorm) # You would like to get info about whole ggplot package help(package=ggplot2)
I know what I want to do but I don’t know R function that implements it.
In this case there are several ways to find what you need.
- You can use
apropos()function. However you should know that it searches for your term only in funcition names.
- To search also in function descriptions you can use
- Functions mentioned above have one critical drawback – they search only in packages that you have loaded. The function you are looking for can be a part of a package that you have no idea about. Fortunately you can use
findFn()function from sos package. It looks for your search term in CRAN repository (the place where R packages are registered).
- Last but not least – do not forget that Google is your friend. R is so popular that in many cases typing “how to do X in R” will give you relevant results.
# look for function that has 'bin' in name apropos('binom') # look for 'binonm' in names and descriptions help.search('binom') # to use findFn() function you need to install the sos package - only for the first time install.packages('sos') # load sos package library(sos) # use findFn() function findFn('normality test', maxPages = 2, sortby="MaxScore") # if you do not want to specify extra arguments you can use ??? instead of findFn ???'Normality test'
I know R function, I know how it works but it doesn’t work as I expected.
That’s the most common situation. First of all use
help(function_name) and read function description to make sure that you use it in a proper way.
If you still have no idea why your code doesn’t work you can search in Google for the problem you’ve got. If this do not help you can ask R Community for help. For statistical questions use stats.stackexchange.com. For programming questions use stackoverflow.com. Remember to be precise and include reproducible example.
If you want to solve a problem on your own, seeing how function is implemented can be helpful. You can see the implementation of any R function by placing cursor within the name of the function name and press F2 button. It works in RStudio. That is the option for advanced R users.
There are no hopeless situations in R programming. We are lucky – R is a popular language and if you tackle with a standard problem there is a significant chance that someone has already solved it. If you are experienced user, do not forget to visit stackoverflow or stack-exchange from time to time to answer beginners’ questions.