R Studio Practical Revision Notes PDF

Title R Studio Practical Revision Notes
Course Introduction to statistical methods in psychology
Institution University College London
Pages 77
File Size 2.5 MB
File Type PDF
Total Downloads 145
Total Views 908

Summary

R Studio Practical Revision Session 2: set up and data wrangling  Learning goals  Learn how to open and save a new R Markdown script  Learn how to set the working directory  Understand the difference between installing and loading packages  Learn how to load data using read()  Learn how to joi...


Description

R Studio Practical Revision Session 2: set up and data wrangling  Learning goals  Learn how to open and save a new R Markdown script  Learn how to set the working directory  Understand the difference between installing and loading packages  Learn how to load data using read.csv()  Learn how to join data using inner_join()  Learn the 6 core dplyr verb functions for data wrangling 

Part 1: Setting up your R session In this section we will prepare our R session by opening a new R markdown script and writing the code to load packages and data sets. These are steps that you will do each time you start a new analysis in R. Step 1: open and save a new R markdown Script  To open a new R Markdown script for this session, go to File (top left hand tab in R Studio) -> New File -> R Markdown.  You usually want to save your script in the same folder as your data sets. Last week you should have saved the happiness data set in a folder on your computer.  Save your new Rmarkdown script by going to File -> Save as and then selecting the folder where your happiness data set is saved (e.g. practicals/Happiness data).  Once you have your script open, delete everything after line 10. When you want to add a new R code chunk click Insert (Top right of your script window) -> R  R Markdown: R markdown scripts are a special type of R script that allow you to combine text with chunks of R code. They can then be ‘knitted’ to create different types of document, including webpages, PDFs and even books. These notes are written in R markdown. Step 2: Set the working directory  Before we load our data, we need to tell R where the data files are located. To do this we need to set the working directory.  Click on Session (top left of R studio) -> set working directory -> choose directory and then select the folder where the data sets and your script are saved.

Step 3: Install and load packages Installing Packages  Packages are add-ons to Rstudio a bit like apps are add-ons to your smartphone. Throughout this course we are going to be using a suite of packages called the ‘Tidyverse’.  The first time you use a package on a particular computer you need to install it (this is equivalent to downloading an app from app store when you want to use a new app on your phone).  Type the following in your Console (bottom left panel):

 Loading Packages  Once a package is installed on your computer, you need to load it when you want to use it (this is equivalent to opening an app on your phone).  This time we are going to include the code in our script, as we will need to run it each time we run the script in a new session. It’s good practice to load all the packages you are going to use in a script at the start.  To add a code chunk go to Insert-> R and then type of following:   #Comments: You’ll notice that the first line in the code chunk above is green, this means it is a comment. You tell R when you are starting a comment by putting a hash tag (#) in front of the text. This tells R it should not try to run the text as code. It is really important to add comments to your script so that you and other people know what you are trying to achieve with each bit of code.  To run the code click the play button (green triangle) at the top right of the R chunk. Step 4: Read in data  The function read.csv() reads in data that are saved in csv files.  The code chunk below reads in the two data sets we looked at last week and assigns them names with the assignment operator.  pinfo...


Similar Free PDFs