R Basics
Welcome to R. Everything below is a live R console β edit the code and press Run Code. Nothing to install.
Watch: R basics in 2 minutes
See every step below run in a live RStudio session β R as a calculator, variables, functions and comments β then try each one yourself in the consoles that follow.
Download the R script Β· βΆ Practice in the playground β or edit and run each snippet below.
R is a calculator
Run this, then change the numbers and run it again:
R evaluates each line and prints the result. ^ is βto the power ofβ.
Store values in variables
Use <- (type it as < then -) to save a value under a name:
Now price and quantity hold values you can reuse. Try adding a discount variable and subtracting it.
Call functions
A function does something to its input, written as name(input):
round(x, 2) rounds to 2 decimals β the parts inside the brackets are called arguments.
Your turn
Change the radius above, or compute something new here:
Next: Vectors β β how R stores and works with many values at once.
Comments
Anything after
#is a comment β R ignores it. Use comments to explain your code: