Lab 1 - Getting Started in RStudio

Author

Joseph Quinn, PhD

Lab 1 - Getting Started in RStudio

Objectives

Primary (be able to do these by the end of the lab)

  • Install R and RStudio

  • Explain the differences between these two pieces of software

Secondary (related to the problem set you’ll turn in by Monday at 11:59pm)

  • Perform basic procedures in the console

  • Seek help using an online resource

  • Write and save your first script

Introduction

SOCY 392 is a practical statistics course. As such, you will be learning how to run statistical analyses using a well-known and increasingly popular programming language called R. Our lectures will emphasize the concepts, logic, and theory behind descriptive and inferential statistics – but you will get to put these lessons into practice each week during your time in lab.

In each week’s lab session, you will use R to apply the lessons you learn in the lecture by completing a weekly problem set. You will complete each problem set with a partner who I randomly selected. During each lab, Chang-Yi will walk you through the basics of each assignment, answer clarifying questions, and help you troubleshoot any coding bugs.

Norms

Most people in this class have not encountered a programming language before. Do not worry: you do not need to become an expert coder to be successful in this course. You do need to develop comfort with independent problem-solving techniques. It takes time and effort to fix a bug or correct a line of code. As a general rule (especially after the first few weeks of class), always spend at least a few minutes on Google, Stack Overflow, or with your partner to see if someone has already answered your question before asking your instructor. 

Even ChatGPT can be a good resource for understanding errors and fixing bugs in your code. You are still acountable to provide explanations of your code in your own words, but you can use ChatGPT as a learning partner. For example, if a specific function is confusing to you and you don’t understand its purpose or know what the specific inputs of the function are, you can paste it into ChatGPT and ask

  • “In simple terms for a new R user, what is the function {insert the function name} supposed to do?”

  • “Can you walk me through the arguments of this function?”

  • “What does my instructor mean when they say that ‘functions’ in R have ‘arguments’?”

  • “What does the n argument in the runif() function in R do?”

  • “I am trying to use the runif() function using this specification: runif(n=-5, min=0, max=-3) but I get the following error {paste your error here}. Walk me through the logic of the function and what I’m doing wrong.”

Note: always ask your instructor to check what ChatGPT is saying if you use it to help you create or edit your code. It is not always right, and your instructor can help explain what it’s saying in the context of the lab even when it does provide a technically correct response.

While there are no dumb questions, there are certainly questions that you can easily answer on your own. Learning how to solve problems independently is an immensely valuable skill, both in academic and professional settings. Employers will want to hear about examples of times where you leveraged resources around you to solve problems through your own troubleshooting techniques. Doing this demonstrates self-sufficiency, and is often the best way to learn a skill. So try to avoid “let me Google that for you” questions about programming tasks. We will always be ready to help once you show us what you have tried to do to solve the problem yourself before coming to us.

Lesson

Today you will install R and RStudio on your laptop. This process is not as simple as installing Chrome - which makes it your first opportunity to troubleshoot semi-independently. Chang-Yi and I will circulate and help, but work with your lab partner and other people around you first if you run into a roadblock.

R vs. RStudio

You are installing two programs on your computer today. The first program is called R. It’s a statistical programming language that allows you to perform all of the analyses we will do in this class (and many, many more). R is the engine of our workflow - it’s the program that makes all of the code you write work. However, you won’t really open it ever - its user interface does not show us all of the information under the hood that we want to see. Because of that, we are installing a second program called RStudio. 

RStudio is a user interface that sits on top of R. It allows us to see what data are loaded, gives us a separate panel for plots and help documentation, and includes two code-related panels. One of these is called the program panel. It is where you write and save your problem sets. The other panel is called the console panel. The console shows us what is happening when we run our script.

Today, our primary goal is to successfully install both programs, and to give you a very brief tour of the RStudio interface. Some people may not be able to complete the installation today. In this event, you will need to see Chang-Yi during his office hours.

Next lab we will talk more about R programming basics, and about getting data into R.

Steps

Primary

  1. First reset your computer.

  2. Figure out what type of computer you have and what type of CPU is inside of it. R installs differently on PCs, Intel-based Macs, and M1-based macs. If you have an Apple computer and aren’t sure which type of Mac you have, click on the Apple symbol in the upper left corner of your desktop and click “About this Mac.” The “Chip” line should say “Intel,” “M1,” “M2,” or “M3” in it. The CPUs that have a “M#” type name are often called “Apple Silicon” because Apple makes them. Remember that for the next steps.

  3. Go to this website, and follow the instructions on installing R and RStudio on your computer. Read the instructions very carefully - especially when it describes differences in the installation process between different types of computers.

Secondary

After you complete the installation, begin on the lab’s secondary goal.

  1. Download lab1script.R and open that script in RStudio (if it does not open in RStudio by default, right-click the file after you download it, Select “Open With,” and choose the RStudio application). The script contains instructions about some basic procedures for writing programs in R.
  2. The script also contains 10 questions for you to answer. Respond to each of these questions by adding your own documentation or code to the script, as each question instructs you to.
  3. Once you are done, save the script using the following naming convention: lab1_lastname_firstname.R. Upload the file to BlackBoard via the “Lab 1” assignment submission page.
  4. If you and your partner finish early, find someone in the classroom who is not done, and ask them if they would like help. The best way to help someone learn how to code is by giving them hints and asking them questions that help them figure out the answer to their problem themselves, so try to use the question-method of helping instead of telling them what to do.