Unformatted text preview:

Lab 1 R Basics and Working with Data Stats 10 Introduction to Statistical Reasoning Summer 2020 All rights reserved Adam Chaffee and Michael Tsiang 2017 2020 Do not post share or distribute anywhere or with anyone without explicit permission Main exercises based on labs by Nicolas Christou and Robert Gould Objectives Before Coming to Lab 1 Demonstrate basic R Skills create vectors perform vector operations install packages 2 Use basic statistical functions mean min max median sd 3 Visualize data dot plot histogram box plot contingency tables scatter plot bubble plot 1 Download the latest version of R at http cran stat ucla edu 2 After R is installed download the latest version of RStudio the free one on the left https www rstudio com products rstudio download 3 Download Introductory Statistics with R by Peter Dalgaard The book is available for free on springerlink com You must access it via a campus computer or using the BOL VPN 4 Read 1 1 and 1 2 1 1 2 3 of the Dalgaard book 5 Consider bringing your own laptop to lab The lab room has computers with RStudio pre installed but you will need to spend time outside of labs to complete your assignments Working on your personal computer can avoid issues with saving and losing files A Primer on labs In Stats 10 the lab courses are designed to help you explore the capabilities of computer programming to assist you in statistical analysis This quarter we will be using R a high level statistical programming language We will cover several basic topics in R to help you carry out analysis that you learn in lecture R is a popular program in universities because it is free and open source which means anyone can access the program s source code and suggest improvements Collaboration Policy In lab you are encouraged to work in pairs or small groups to discuss the concepts on the assignments However DO NOT copy each other s work as this constitutes cheating The work you submit must be entirely your own If you have a question in lab feel free to reach out to other groups or talk to your TA if you get stuck Section 1 R and RStudio Basics Instructions When you open RStudio you ll notice the window is partitioned into four sections The top left section is your working code file This is where you should type and run your commands It serves two main purposes o You can edit and save this section just like a word document In fact let s save right now Go to file save and save this in a convenient location on your computer o You will run lines of code by highlighting the code chunk you want to run and clicking the run button shortcut ctrl enter for PC cmd enter for Mac The bottom left section is the console This is essentially the R engine that runs your commands and produces output You should not need to type anything in this section but you will need to copy and paste output for exercises The top right section defines your environment The environment contains any objects you create and data you import You won t use it much but it s a quick way to see what data and objects you have to work with The bottom left section contains three important tabs in this course o Plots when you create plots they will show up here You should copy and paste these to answer some of the lab exercises o Packages R contains packages that you can install and run to use cool features and functions that the basic R install did not give you o Help If you are unsure about what a function does try searching for it in the help tab You will find information about the function arguments and examples Working with packages To work with packages we must do two things 1 Install the package by using the packages tab mentioned above 2 Type and run library package name to tell R to load the package we installed Reading Data into RStudio R needs to be told which folder to look in to retrieve data To read in data first download it into a convenient folder on your machine Then try one of these methods 1 At the top of RStudio go to Session set working directory choose directory Then select the folder the data is saved in and click Select Folder Then create a new data object by using the syntax object name read csv file filename csv 2 Use the syntax object name read csv file choose and use the file explorer to manually select the downloaded data Vectors Examples Try running the commands below The first creates a numeric vector object called numbers that contains the numbers 1 to 5 The second creates a character vector object called schools that contains the names of two top tier colleges and the name of a terrible school for spoiled children Note that when you create a vector of names you must put quotes around each element The c function is used to create both vectors Try typing c in the help menu to get a better sense of what this function does numbers c 1 2 3 4 5 schools c UCLA UC Berkeley USC Note that when you run these commands there is no output That is because whenever you define an object there is not output However once you have made the object you can print the contents of an object by typing the name of the object Example type numbers If the vector is numeric we can do math on it For example try typing numbers 2 We can also use square brackets to create subsets of our data For example if you type schools 2 you will retrieve only the second element from the schools vector Object classes Vectors matrices and data frames are three common object classes you will work with Matrices and data frames can be thought of as tables of data However matrices contain only numeric data Data frames can contain several types of data The file you will work with in this lab is a sample of information about babies born in North Carolina It is considered a data frame because it contains numeric information about each baby as well as various pieces of categorical data such as the race of the parents and whether the mom has a smoking habit Section 1 exercises 1 Vectors a Create a vector named heights that contains the heights in inches of yourself and two students near you Print the contents of this vector b Create a vector named names that contains the names of these people Print the contents c Try typing cbind heights names What did this command do What class is this of this vector new object Hint Try the class function 2 Downloading data a Download the data set births csv from the CCLE site and upload it into RStudio Name the data frame NCbirths b Demonstrate that you have been successful by typing head


View Full Document

UCLA STATS 10 - Lab 1: R Basics and Working with Data

Download Lab 1: R Basics and Working with Data
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lab 1: R Basics and Working with Data and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lab 1: R Basics and Working with Data 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?