Course Outline
-
segmentGetting Started (Don't Skip This Part)
-
segmentAlgebra + Data Science
-
segmentChapter 1 - Exploring Variation in Data
-
1.2 Introduction to R Functions
-
segmentChapter 2 - Modeling Data with Functions
-
segmentChapter 3 - Assessing How Well Models Fit the Data
-
segmentResources
list High School / Algebra + Data Science (G)
1.2 Introduction to R Functions
How to Learn the Most from the Coding Exercises
The <Run> button will run your code in the code window. The <Submit> button will both run the code and submit your answer to be graded. You’ll learn the most by trying to write code, running it, and keeping on trying until it works. After you’ve figured it out, click <Submit>.
Feel free to try out different ideas, even after you’ve gotten the code to run. You can keep running code even after you have clicked <Submit>. The more you explore, the more you will learn. Sometimes coding will feel frustrating, like a puzzle that is hard to solve. But being frustrated is actually a normal part of programming; it’s part of getting better!
Introduction to R Functions
So far you know how to print some words and do some basic arithmetic in R. One of the great things about R is that there are a lot of built in commands that you can use. These are called functions. You have already seen two functions in action, print()
and sum()
.
Functions have two basic parts. The first part is the name of the function (e.g., sum
). The second part is the input to the function, which goes inside the parentheses. In coding, we call these inputs arguments. Here we’ve put in some instructions (as comments) into the code window. Write your code as a new line under each comment. See if your code works by clicking <Run>. If it works, click <Submit>.
# Use the sum() function to add the numbers 5, 10, 15
# Use the print() function to print the word "hello"
# Use the sum() function to add the numbers 5, 10, 15
sum(5, 10, 15)
# Use the print() function to print the word "hello"
print("hello")
ex() %>% {
check_function(., 'sum') %>% {
check_arg(., "...") %>% check_equal()
check_result(.) %>% check_equal()
}
check_function(., 'print') %>%
check_result() %>% check_equal()
}
Notice that the actual R code are the lines you wrote in the code window, such as sum(5,10,15)
or print("hello")
. The output or result of the code (e.g., 30) appears in a new area underneath the buttons after you click <Run>.
R is Picky; Sorry About That!
One thing to be aware of is that R is very, very picky. For example, if you type sum(1,100)
it will tell you the answer, 101. But if you type Sum(1,100)
, capitalizing the “s,” it will act like it has no idea what you are talking about!
To take another example: in the print()
function, if we left off the quotation marks, typing print(hello)
instead of print("hello")
, R would return an error message. Let us show you what we mean.
# Run the code below by pressing Run
# Now debug the code - fix the mistakes - and press Run
# (When you have it correct don't forget to Submit)
Sum(1, 2)
print(hello)
# Run the code below by pressing Run
# Now debug the code - fix the mistakes - and press Run
# (When you have it correct don't forget to Submit)
sum(1, 2)
print("hello")
ex() %>% {
check_function(., 'sum') %>% {
check_arg(., "...") %>% check_equal()
check_result(.) %>% check_equal()
}
check_function(., 'print') %>%
check_result() %>% check_equal()
}
If a human treated you this way it would be infuriating! A human would figure out what you meant. But R, a computer program, is not able to do that. It assumes you mean exactly what you type.
You Can’t Possibly Memorize All of R’s Functions
Even though you will learn some R functions in this class, there are literally thousands of functions in R, more than anyone could remember. And, there are often many functions that do similar things. Even advanced users of R can’t remember it all.
You’ll learn a lot of functions as you progress through this course. Instead of trying to memorize them, it may be helpful to keep track of them in a notebook. You can also take notes on our R cheatsheet. You can download it either as a PDF or Word docx file. (You can also find more cheatsheets in the Resources section at the end of the course materials. Scroll down past all the chapters.)
We also search the internet for functions we can’t remember. Literally: Type your question into Google and press <Enter>. You’ll often see a bunch of helpful forums where other R users post potential answers to your question. Not only will you find some new functions, but you’ll also find endless discussions about which ones are better than others. Oh, what fun!
Trial and Error, and the Culture of Programming
The best way to learn programming is to try things and see what happens. Write some code, run it, and think about why it didn’t work! (Sorry to be negative, but often things don’t work the first time.) There are so many ways to make tiny mistakes in programming (e.g., misspelling the name of a function or forgetting to close your parentheses). We often have to find these bugs by trial and error.
Trial and error can be frustrating if we are not used to learning this way, and it may seem inefficient. But trial and error is a great way to learn because we learn from wrong answers as well as right ones. In this course we might sometimes ask you to run code that is wrong just to see what happens!