-
Notifications
You must be signed in to change notification settings - Fork 0
Basics of R
R is a free open-source computer language and environment for statistics and visualizations. It is a higher level language, meaning it makes assumptions and has intuitions to make the user experience simpler, however this is at the cost of computational speed.
- R Console (left): keeps a log of what lines of script you have run and their outputs. You can't manipulate anything in its history, only read it. Anything after the
>
is a line of code you've run. - You may run commands from the console, but it is not recommended for the bulk of your work. That is what scripts are for.
Choosing "File > New Document" opens up the script editor.
- Script editor (right): allows you to edit scripts (".R" files). Scripts are a set of instructions - a recipe - that you give R to execute.
R Studio is a user interface for R that provides more graphical features. When you open R Studio you will see 4 panels. These include a script editor (here, top left) and an R console (bottom left) like in base R. The other two are:
- Environment and History (top right): an easy way to see all the R objects you've created and stored
- Plots and Help (bottom right): where you can view plots and help screens that you generate
A script is a series of commands that is executed by R to achieve author's goals.
- In R, scripts are saved as .R files
- You can open and save them like any other file on your computer
- You can access other scripts within a script by using the
source()
command - You can add comments, text in your script that R will not run, using a #
There are two ways to run lines of script you've written: using the "run" button in the menu or pressing Ctrl+Enter (Cmd+Enter). If nothing is highlighted, doing either action will automatically run only the line of code your cursor is on. You can also highlight any amount of code and it will run only what is highlighted.
Your biggest collaborator is you six months ago, and you no longer answer emails.
You will learn the hard way that you can never annotate your code enough. No matter how much you think you will remember what you wrote, you will never remember it as well as you did the moment you wrote it - so save yourself some time and leave comments that make it easier for you and others to reproduce your work.
You can add comments using the #
symbol. R does not run anything in a line that comes after at least one pound symbol.
# By starting a line with a, #, R treats it as a comment and will ignore the text
# This is the method for making notes to yourself or other users of the script
# run me:
2 + 2 # will all this text mess up R's arithmetic?
- Open a new R script in RStudio.
- Write a comment at the top explaining what this file is.
- Save your file to a folder on your Desktop called "RCourse"
Scripts are the foundation of R, and you want them to be interpretable to yourself and others. This will make your results easily reproducible and interpretable. Like any language, it is important to learn the "grammar" and establish good habits early.
Google's R style guide is a good place to start, but there are also other blogs and websites to give you tips. https://google.github.io/styleguide/Rguide.xml
Commenting Guidelines Comment your code. Entire commented lines should begin with # and one space. Short comments can be placed after code preceded by two spaces, #, and then one space. Again, comment your code. This is essential to remember what a script does, especially when you have not used it in a year and to make your scripts interpretable to other people.
Day 1 or 2
- Basics of R
- Variables and objects
- Functions and packages
- Conditionals and logical operators
- Practice
- Recap
Day 3
Day 4
Day 5
Extra