Course Outline
-
segmentGetting Started (Don't Skip This Part)
-
segmentStatistics and Data Science II
-
segmentPART I: EXPLORING AND MODELING VARIATION
-
segmentChapter 1 - Exploring Data with R
-
1.2 Getting Started with R
-
segmentChapter 2 - From Exploring to Modeling Variation
-
segmentChapter 3 - Modeling Relationships in Data
-
segmentPART II: COMPARING MODELS TO MAKE INFERENCES
-
segmentChapter 4 - The Logic of Inference
-
segmentChapter 5 - Model Comparison with F
-
segmentChapter 6 - Parameter Estimation and Confidence Intervals
-
segmentPART III: MULTIVARIATE MODELS
-
segmentChapter 7 - Introduction to Multivariate Models
-
segmentChapter 8 - Multivariate Model Comparisons
-
segmentChapter 9 - Models with Interactions
-
segmentChapter 10 - More Models with Interactions
-
segmentFinishing Up (Don't Skip This Part!)
-
segmentResources
list High School / Statistics and Data Science II (XCD)
1.2 Getting Started with R
To begin, let’s introduce you to our favorite letter: R. Why is R our favorite? Because ‘R’ is the name of a powerful, free coding language commonly used by statisticians and data scientists around the world. It’s also the main coding language taught in this book! Once you learn how to use it (or, for familiar users, once you refresh your memory of using it), R will make analyzing data easier, faster, and more understandable.
Why R?
Modern data analysis often involves large datasets - sometimes with millions or even billions of values! To make meaning out of such complex datasets, we need the proper tool for the job. R is that very tool. It’s a powerful coding language built specifically for statistical tasks. Moreover, because it’s free, open source, and widely used, analyzing data using R will help make your work more understandable and make it easy for others to reproduce your results. Plus, if you ever need help, there is a worldwide community of R users to support you on the internet.
Thankfully, though, you won’t have to look that far for now. This book will take you through learning R step by step, slowly. And you won’t have to install anything or do anything special to your computer. You can just focus on learning R.
Try Some R Code
Here’s a bit of R (what we sometimes refer to as “code”) in the window below. What do you think it will do? (Note: Press the <Connect> button to load all the code windows on this page; it may take 1-2 minutes. The code window is ready when you see a blue dot and the word Ready to the right of the <Submit> button.)
Press the <Run> button and see what happens.
print("Hello world!")
print("Hello world!")
ex() %>%
check_function("print") %>%
check_arg("x") %>%
check_equal()
IF THE CODE WINDOW DOESN’T WORK: Try following the code window’s instructions (in particular – don’t refresh if it tells you not to refresh!). You might also try waiting a few minutes then pressing the <Reconnect> button. If that doesn’t work, go back to the First Things First page at the beginning of the book to review your technology setup. Then try refreshing your browser page.
If you still can’t get it to work, click the diamond-shaped CK icon in the lower right corner of this page to file a tech support ticket. This will also give you access to a knowledge base, including a searchable list of all R functions used in the book and the page on which they are first introduced.
After you click the <Run> button, you will see that R displays the phrase “Hello world!” in an area below the <Run> and <Submit> buttons. When we tell R to print()
, R interprets that to mean, “Display on the screen.” You just figured out a little bit of R.
Important Things to Notice About the Code Window
There are a few things worth noting about the way the code window works.
<Run> versus <Submit> buttons. When you press the <Run> button it will run all the code in the window above the button. You can run and re-run code as many times as you want. But to get credit for doing the assignment, and to get some feedback, you need to press the <Submit> button.
Go back to the code window above and press <Submit>. This time you get a blue checkmark and some feedback, depending on whether you succeeded in the code exercise or not. Be sure to submit your final work for each code window to your instructor by pressing <Submit> (unless no <Submit> button is available.)
<Reset> button. The white <Reset> button on the right side of the code window will delete the work you have done so far and return the window to its original state. It’s a good button to push if you want to start over, or just try again without looking at your previous solution.
Try Some More R Code
Let’s try another one. Read the code and see if you can guess what it will do. Then press the <Run> button.
sum(100, 500, 23)
sum(100, 500, 23)
ex() %>%
check_function("sum") %>%
check_arg("...", arg_not_specified_msg = "Make sure you don't delete what's inside the parentheses.") %>%
check_equal(incorrect_msg = "Make sure you don't change what's inside the parentheses.")
This bit of code printed out the sum of 100, 500, and 23 (that is, 623).
You can also use R like a basic calculator. Take a look at the math in the code window below then just press <Run>.
# a few basic arithmetic things
5 + 1
10 - 3
2 * 4
9 / 3
# a few basic arithmetic things
5 + 1
10 - 3
2 * 4
9 / 3
ex() %>% {
check_operator(., "+") %>% check_result() %>% check_equal()
check_operator(., "-") %>% check_result() %>% check_equal()
check_operator(., "*") %>% check_result() %>% check_equal()
check_operator(., "/") %>% check_result() %>% check_equal()
}
Notice that you can put more than one line of code—or set of instructions—in a single R window. When you press the <Run> button, all the commands in the window will be run, one after the other, in the order in which they appear.
Comments in the R Window
Sometimes we will write things in the R coding window that we want R to ignore. These are called comments and they start with a #
. R will ignore comments, and just execute the code. In this book we will use the comments as a way to give you instructions for R exercises. In the code window below, try typing whatever you want after a #
at the front of the line. Then press <Run>.
require(coursekata)
# type whatever you want
# see... blah blah blah
# no solution, but need code to show submit button
ex() %>% check_code("#", fixed = TRUE)
Notice that you don’t see anything happen because lines that start with a #
are ignored by R.
Comments are particularly helpful when sharing code with collaborators. They allow you to explain each part of your code, without interfering with how it runs.
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. And if you feel frustrated, that just goes with the territory. Learn to enjoy your frustration; it’s part of getting better!
A Code Window Sandbox is Always Available
We will always provide a code window when you need one. But, sometimes you may just want to try something out.
Go to the Resources folder at the end of the textbook and click on the page that says R Sandbox. This will open a page with an empty code window. This gives you a handy place to run some R code.