First steps in R
Learn some of the fundamental code for creating and manipulating data in R
For all these exercises:
- Copy-paste the code from the slides into the interactive playground below
- 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 , or see the end of our RStudio instructions
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:
- Copy-paste the example code into a text file, preferably with a
.R
extension to create an R script
- 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 offunction_name
- R is case-sensitive1
Footnotes
Case-sensitive: A capital letter is not the equivalent of a lower-case letter↩︎