First steps in R

Learn some of the fundamental code for creating and manipulating data in R

Modified

August 13, 2024

For all these exercises:

  1. Copy-paste the code from the slides into the interactive playground below
  2. Try editing and re-running it - play with the examples to reinforce what you’re learning
Running code

Once you’ve typed the code, run it with the Run Code button Run code button, or see the end of our RStudio instructions

View slides in full screen

Save your code in a script

It’s good to get into the habit of writing your code into a text file, to keep a clean copy, separate from any outputs that R generates

Keep a record of what you learn!

Whether you’re running code in R/RStudio or in the browser, we recommend you save the code for future reference:

  1. Copy-paste the example code into a text file, preferably with a .R extension to create an R script
  2. Annotate the code with your own comments, to remind yourself what each line means. Add comments by starting the line with a hash symbol #, as in our examples

Common coding mistakes

If your code isn’t working, check for these common errors:

  • Missing closing parenthesis ) - if R shows you a + prompt instead of the usual > prompt, this is probably the reason
  • Using the wrong case for function or object names e.g. Function_Name instead of function_name - R is case-sensitive1

Green and yellow painting of a toddler taking their first steps in a cottage garden

Back to top

Footnotes

  1. Case-sensitive: A capital letter is not the equivalent of a lower-case letter↩︎