Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

R Projects and Data Import in RStudio: A Comprehensive Guide, Study notes of Statistics

A step-by-step guide on creating and managing R projects, scripts, and data in RStudio. It covers creating an R project, organizing scripts, housekeeping, commenting, and importing data. It also discusses saving and exporting data, preparing data for R, and handling broken data.

What you will learn

  • What is the purpose of creating scripts in R projects?
  • What are the best practices for preparing data for R?
  • How do I handle broken data in R?
  • How do I create an R project in RStudio?
  • How do I import data into R?

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

larryp
larryp ๐Ÿ‡บ๐Ÿ‡ธ

4.8

(34)

353 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data and
Projects in R-
Studio
Material prepared in part by
Etienne Low-Decarie, Zofia
Taranu, Patrick Thompson
Vaughn DiMarco
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download R Projects and Data Import in RStudio: A Comprehensive Guide and more Study notes Statistics in PDF only on Docsity!

Data and

Projects in R-

Studio

Material prepared in part by Etienne Low-Decarie, Zofia Taranu, Patrick Thompson Vaughn DiMarco

Learning Objectives

ย›๏‚› Create an R project ย›๏‚› Look at Data in R ย›๏‚› Create data that is appropriate for use with R ย›๏‚› Import data ย›๏‚› Save and export data

Create an R project ย›๏‚› A folder in R-Studio for all your work on one project (eg. Thesis chapter) ย›๏‚› Helps you stay organized ย›๏‚› R will load from and save to here

Create an R project in RStudio ย›๏‚› In R Studio, select create project from the project menu (top right corner)

The Scripts

ย›๏‚› What is this ?"

  • A text file that contains all the commands you will use

ย›๏‚› Once written and saved , your script file allows

you to make changes and re-run analyses with

minimal effort!

Create an R script

R Projects can have multiple scripts ย›๏‚› Open the file: Data and Projects in R-Studio.R ย›๏‚› this script has all of the code from this workshop ย›๏‚› Recommendation ย›๏‚› type code into the blank script that you created ย›๏‚› refer to provided code only if needed ย›๏‚› avoid copy pasting or running the code directly from our script

Why use R Projects?" ย›๏‚› Close and reopen your R Project ย›๏‚› In the R โ€“ Projects menu ย›๏‚› Both our script and the one you created will open automatically ย›๏‚› Great way to organize your various projects

ย›๏‚› Remove all variables from memory ย›๏‚› Prevents errors such as use of older data ย›๏‚› Demo โ€“ add some test data to your workspace and then see how rm(list=ls()) removes it A<-โ€Testโ€

Clear R workspace

rm(list = ls() ) Type this in your R sc ript

Housekeeping

ย›๏‚› # symbol tells R to ignore this ย›๏‚› commenting/documenting ย›๏‚› annotate someoneโ€™s script is good way to learn ย›๏‚› remember what you did ย›๏‚› tell collaborators what you did ย›๏‚› good step towards reproducible science

Commenting

ย›๏‚› Tells R where your scripts and data are ย›๏‚› type โ€œgetwd()โ€ in the console to see your working directory ย›๏‚› RStudio automatically sets the directory to the folder containing your R project ย›๏‚› a โ€œ/โ€ separates folders and file ย›๏‚› You can also set your working directory in the โ€œsessionโ€ menu

Working Directory

ย›๏‚› You can have sub directories within your working directory to organize your data, plots, ectโ€ฆ ย›๏‚› โ€œ./โ€ โ€“ will tell R to start from the current working directory ย›๏‚› Eg. We have a folder called โ€œDataโ€ in our working directory ย›๏‚› โ€œ./Dataโ€ โ€“ will tell R that you want to access files in that folder

Sub Directories

Notice that R-Studio now provides information on the CO2 data

Importing Data

look at the whole dataframe look at the first few rows names of the columns in the dataframe structure of the dataframe attributes of the dataframe number of columns number of rows summary statistics plot of all variable combinations CO head(CO2) names(CO2) str(CO2) attributes(CO2) ncol(CO2) nrow(CO2) summary(CO2) plot(CO2)

Looking at Data