Hello, R
R is the language of statistics and data analysis, used by researchers, governments and data scientists around the world.
R works as a calculator straight away:
2 + 3 # [1] 5
10 / 4 # [1] 2.5
2 ^ 10 # [1] 1024
sqrt(81) # [1] 9
Typing an expression on its own line prints its value. The [1] is R telling you this is the first item of the result (more on that next lesson).
Variables
R's traditional way to assign is the arrow <-:
harvest_kg <- 1200
price_per_kg <- 0.45
income <- harvest_kg * price_per_kg
income # [1] 540
(= also works, but <- is what you will see in most R code.) Variable names can contain dots and underscores: total.income, total_income.
Printing
print(x)prints a value the standard R way, with the[1].cat(...)prints plain text, joining its arguments with spaces. Add"\n"for a new line.
cat("Income:", income, "\n") # Income: 540
round(x, 2) rounds to 2 decimal places.
The first run downloads R itself (about 25 MB), so it takes a while; after that, runs are quick.
Your task
- Print
Hello, R!withcat. - Create
rainfall_mmwith820anddayswith365. - Print
Average per day: 2.25: the rainfall per day, rounded to 2 decimals.