Course Outline

list High School / Statistics and Data Science II (XCD)

Book
  • College / Advanced Statistics and Data Science (ABCD)
  • College / Statistics and Data Science (ABC)
  • High School / Advanced Statistics and Data Science I (ABC)
  • High School / Statistics and Data Science I (AB)
  • High School / Statistics and Data Science II (XCD)

1.3 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. 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() }
CK Code: X1_Code_IntroR_01

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 mistake and press Run # (When you have it correct don't forget to Submit) Sum(1, 2) # Run the code below by pressing Run # Now try debugging the code - fix the mistake press Run again sum(1, 2) ex() %>% check_function('sum') %>% check_result() %>% check_equal()
CK Code: X1_Code_IntroR_02

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’ll learn a lot of functions as you progress through this course. It may be helpful to keep track of them in a notebook. We’ve also provided an R cheat sheet that you can mark up as you go through the course. (You can also find it in the Resources section at the end of the course materials.)

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.

What we do—and you can do it too—is just search on 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., writing an uppercase letter when you need a lowercase letter). 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!

Responses