# if using VSCode may been
#install.packages("languageserver")
hist(mtcars$mpg)
Sample R lesson
You can define sections in your quarto using html.
<div class="questions">
</div>
Questions
- What can R do?
- How do I do it?
Objectives
- Learn the basic R commands
Challenge
Write a function to convert map scale. For example, on a 1:25,000 map (good for hiking!) the distance between two points is 15 cm. How far apart are these in real life? (3750 m).
[Reminder: 15 cm * 25000 = 375000 cm = 3750 m]
Your function should take as input two numbers: the distance on the map (in cm) and the second number of the scale and, i.e. calculate_distance(15, 25000)
should return 375000
Solution
#Declare the name of the function
# this is not a R example - but you get the idea!
calculate_distance(distance_cm,scale):
def '''calculates distance based on map and scale
returns: z
inputs: distance_cm,scale
distance_cm and scale are two integers
returns the product of distance_cm and scale
'''
return(distance_cm * scale)
Key points
- You can store things in R in variables
- Lists can be used to store objects of different types
- Loops with for can be used to iterate over each object in a list
- Functions are used to write (and debug) repetitive code once
- Indexing
All materials copyright Sydney Informatics Hub, University of Sydney