9.2 The Working Directory - YaRrr! The Pirate's Guide To R
Maybe your like
- YaRrr! The Pirate's Guide to R
- 1 Preface
- 1.1 Where did this book come from?
- 1.2 Who is this book for?
- 1.3 Why is R so great?
- 1.4 Why R is like a relationship…
- 1.5 R resources
- 1.5.1 R Cheatsheets
- 1.5.2 Getting R help and inspiration online
- 1.5.3 Other R books
- 1.6 Who am I?
- 1.6.1 Please consider a donation!
- 1.6.2 Acknowledgements
- 1.7 Contributions and Acknowledgements
- 2 Getting Started
- 2.1 Installing Base-R and RStudio
- 2.1.1 Check for version updates
- 2.2 The four RStudio Windows
- 2.2.1 Source - Your notepad for code
- 2.2.2 Console: R’s Heart
- 2.2.3 Environment / History
- 2.2.4 Files / Plots / Packages / Help
- 2.3 Packages
- 2.3.1 Installing a new package
- 2.3.2 Loading a package
- 2.4 Reading and writing Code
- 2.4.1 Code Chunks
- 2.4.2 Comments with #
- 2.4.3 Element numbers in output [1]
- 2.5 Debugging
- 2.5.1 R is not ready (>)
- 2.5.2 Misspelled object or function
- 2.5.3 Punctuation problems
- 2.1 Installing Base-R and RStudio
- 3 Jump In!
- 3.1 Exploring data
- 3.2 Descriptive statistics
- 3.3 Plotting
- 3.4 Hypothesis tests
- 3.5 Regression analysis
- 3.6 Bayesian Statistics
- 3.7 Wasn’t that easy?!
- 4 The Basics
- 4.1 The command-line (Console)
- 4.2 Writing R scripts in an editor
- 4.2.1 Send code from an source to the console
- 4.3 A brief style guide: Commenting and spacing
- 4.3.1 Commenting code with the # (pound) sign
- 4.3.2 Spacing
- 4.4 Objects and functions
- 4.4.1 Numbers versus characters
- 4.4.2 Creating new objects with <-
- 4.4.3 How to name objects
- 4.4.4 Example: Pirates of The Caribbean
- 4.5 Test your R might!
- 5 Scalars and vectors
- 5.1 Scalars
- 5.2 Vectors
- 5.2.1 a:b
- 5.2.2 seq()
- 5.2.3 rep()
- 5.3 Generating random data
- 5.3.1 sample()
- 5.3.2 Normal (Gaussian)
- 5.3.3 Uniform
- 5.3.4 Notes on random samples
- 5.4 Test your R might!
- 6 Vector functions
- 6.1 Arithmetic operations on vectors
- 6.1.1 Basic math with multiple vectors
- 6.1.2 Ex: Pirate Bake Sale
- 6.2 Summary statistics
- 6.2.1 length()
- 6.2.2 Additional numeric vector functions
- 6.2.3 Sample statistics from random samples
- 6.3 Counting statistics
- 6.4 Missing (NA) values
- 6.5 Standardization (z-score)
- 6.5.1 Ex: Evaluating a competition
- 6.6 Test your R Might!
- 6.1 Arithmetic operations on vectors
- 7 Indexing Vectors with [ ]
- 7.1 Numerical Indexing
- 7.2 Logical Indexing
- 7.2.1 & (and), | (or), %in%
- 7.2.2 Counts and percentages from logical vectors
- 7.2.3 Additional Logical functions
- 7.3 Changing values of a vector
- 7.3.1 Ex: Fixing invalid responses to a Happiness survey
- 7.4 Test your R Might!: Movie data
- 8 Matrices and Dataframes
- 8.1 What are matrices and dataframes?
- 8.2 Creating matrices and dataframes
- 8.2.1 cbind(), rbind()
- 8.2.2 matrix()
- 8.2.3 data.frame()
- 8.2.4 Dataframes pre-loaded in R
- 8.3 Matrix and dataframe functions
- 8.3.1 head(), tail(), View()
- 8.3.2 summary(), str()
- 8.4 Dataframe column names
- 8.4.1 Adding new columns
- 8.4.2 Changing column names
- 8.5 Slicing dataframes
- 8.5.1 Slicing with [, ]
- 8.5.2 Slicing with logical vectors
- 8.5.3 Slicing with subset()
- 8.6 Combining slicing with functions
- 8.6.1 with()
- 8.7 Test your R might! Pirates and superheroes
- 9 Importing, saving and managing data
- 9.1 Workspace management functions
- 9.1.1 Why object and file management is so important
- 9.2 The working directory
- 9.3 Projects in RStudio
- 9.4 The workspace
- 9.4.1 ls()
- 9.5 .RData files
- 9.5.1 save()
- 9.5.2 save.image()
- 9.5.3 load()
- 9.5.4 rm()
- 9.6 .txt files
- 9.6.1 write.table()
- 9.6.2 read.table()
- 9.6.3 Reading files directly from a web URL
- 9.7 Excel, SPSS, and other data files
- 9.8 Additional tips
- 9.9 Test your R Might!
- 9.1 Workspace management functions
- 10 Advanced dataframe manipulation
- 10.1 order(): Sorting data
- 10.2 merge(): Combining data
- 10.3 aggregate(): Grouped aggregation
- 10.4 dplyr
- 10.4.1 Additional dplyr help
- 10.5 Additional aggregation functions
- 10.5.1 rowMeans(), colMeans()
- 10.5.2 apply family
- 10.6 Test your R might!: Mmmmm…caffeine
- 11 Plotting (I)
- 11.1 Colors
- 11.1.1 Colors by name
- 11.1.2 gray()
- 11.1.3 yarrr::transparent()
- 11.2 Plotting arguments
- 11.3 Scatterplot: plot()
- 11.3.1 Symbol types: pch
- 11.4 Histogram: hist()
- 11.5 Barplot: barplot()
- 11.5.1 Clustered barplot
- 11.6 pirateplot()
- 11.6.1 Pirateplot themes
- 11.6.2 Customizing pirateplots
- 11.6.3 Saving output
- 11.7 Low-level plotting functions
- 11.7.1 Starting with a blank plot
- 11.7.2 points()
- 11.7.3 abline(), segments(), grid()
- 11.7.4 text()
- 11.7.5 Combining text and numbers with paste()
- 11.7.6 curve()
- 11.7.7 legend()
- 11.8 Saving plots to a file with pdf(), jpeg() and png()
- 11.9 Examples
- 11.10 Test your R might! Purdy pictures
- 11.1 Colors
- 12 Plotting (II)
- 12.1 More colors
- 12.1.1 piratepal()
- 12.1.2 RColorBrewer
- 12.1.3 colorRamp2
- 12.1.4 Getting colors with a kuler
- 12.2 Plot Margins
- 12.3 Arranging plots with par(mfrow) and layout()
- 12.3.1 Complex plot layouts with layout()
- 12.4 Additional plotting parameters
- 12.1 More colors
- 13 Hypothesis Tests
- 13.1 A short introduction to hypothesis tests
- 13.1.1 Null v Alternative Hypothesis
- 13.1.2 Descriptive statistics
- 13.1.3 Test Statistics
- 13.1.4 p-value
- 13.2 Hypothesis test objects: htest
- 13.3 T-test: t.test()
- 13.3.1 1-sample t-test
- 13.3.2 2-sample t-test
- 13.4 Correlation: cor.test()
- 13.5 Chi-square: chsq.test()
- 13.5.1 Getting APA-style conclusions with the apa function
- 13.6 Test your R might!
- 13.1 A short introduction to hypothesis tests
- 14 ANOVA
- 14.1 Full-factorial between-subjects ANOVA
- 14.1.1 What does ANOVA stand for?
- 14.2 4 Steps to conduct an ANOVA
- 14.3 Ex: One-way ANOVA
- 14.4 Ex: Two-way ANOVA
- 14.4.1 ANOVA with interactions
- 14.5 Type I, Type II, and Type III ANOVAs
- 14.6 Getting additional information from ANOVA objects
- 14.7 Repeated measures ANOVA using the lme4 package
- 14.8 Test your R might!
- 14.1 Full-factorial between-subjects ANOVA
- 15 Regression
- 15.1 The Linear Model
- 15.2 Linear regression with lm()
- 15.2.1 Estimating the value of diamonds with lm()
- 15.2.2 Getting model fits with fitted.values
- 15.2.3 Using predict() to predict new data from a model
- 15.2.4 Including interactions in models: y ~ x1 * x2
- 15.2.5 Center variables before computing interactions!
- 15.2.6 Getting an ANOVA from a regression model with aov()
- 15.3 Comparing regression models with anova()
- 15.4 Regression on non-Normal data with glm()
- 15.5 Logistic regression with glm(family = "binomial"
- 15.5.1 Adding a regression line to a plot
- 15.5.2 Transforming skewed variables prior to standard regression
- 15.6 Test your might! A ship auction
- 16 Custom functions
- 16.1 Why would you want to write your own function?
- 16.2 The structure of a custom function
- 16.2.1 Creating my.mean()
- 16.2.2 Specifying multiple inputs
- 16.2.3 Including default values for arguments
- 16.3 Using if, then statements in functions
- 16.4 A worked example: plot.advanced()
- 16.4.1 Seeing function code
- 16.4.2 Using stop() to completely stop a function and print an error
- 16.4.3 Using vectors as arguments
- 16.4.4 Storing and loading your functions to and from a function file with source()
- 16.4.5 Testing functions
- 16.4.6 Using ... as a wildcard argument
- 16.5 Test your R might!
- 17 Loops
- 17.1 What are loops?
- 17.1.1 Printing numbers from 1 to 100
- 17.1.2 Adding the integers from 1 to 100
- 17.2 Creating multiple plots with a loop
- 17.3 Updating a container object with a loop
- 17.4 Loops over multiple indices with a design matrix
- 17.5 The list object
- 17.6 Test your R might!
- 17.1 What are loops?
- 18 Solutions
- 18.1 Chapter 4: The Basics
- 18.2 Chapter 5: Scalers and vectors
- 18.3 Chapter 6: Vector Functions
- 18.4 Chapter 7: Indexing vectors with []
- 18.5 Chapter 8: Matrices and Dataframes
- 18.6 Chapter 13: Hypothesis tests
- 18.7 Chapter 14: ANOVA
- 18.8 Chapter 15: Regression
- References
- Published with bookdown
9.2 The working directory
Figure 9.3: A working directory is like a flag on your computer that tells R where to start looking for your files related to a specific project. Each project should have its own folder with organized sub-folders.
The working directory is just a file path on your computer that sets the default location of any files you read into R, or save out of R. In other words, a working directory is like a little flag somewhere on your computer which is tied to a specific analysis project. If you ask R to import a dataset from a text file, or save a dataframe as a text file, it will assume that the file is inside of your working directory.
You can only have one working directory active at any given time. The active working directory is called your current working directory.
To see your current working directory, use getwd():
# Print my current working directory getwd() ## [1] "/Users/nphillips/Dropbox/manuscripts/YaRrr/YaRrr_bd"As you can see, when I run this code, it tells me that my working directory is in a folder on my Desktop called yarrr. This means that when I try to read new files into R, or write files out of R, it will assume that I want to put them in this folder.
If you want to change your working directory, use the setwd() function. For example, if I wanted to change my working directory to an existing Dropbox folder called yarrr, I’d run the following code:
# Change my working directory to the following path setwd(dir = "/Users/nphillips/Dropbox/yarrr")Tag » How To Set Working Directory In R
-
Running RStudio And Setting Up Your Working Directory - STHDA
-
Getwd, Setwd | R Function Of The Day
-
How To Change Working Directory In R - Universe Of Data Science
-
Working Directories And Workspaces In The RStudio IDE
-
SET And GET Working Directory In R [setwd() And Getwd() Functions]
-
Get Or Set Working Directory - R
-
1.7 Working Directories | An Introduction To R
-
Setting Up Working Directory In R | R Tutorial 1.11 - YouTube
-
Changing Working Directories In R | R - DataCamp
-
Chapter 12 Setting A Working Directory | R For HR: An Introduction To ...
-
Get And Set Working Directory In R - GeeksforGeeks
-
How To Set Working Directory | Introduction To Quantitative Methods
-
Set Working Directory To Source File Location In RStudio ...
-
7. Import And Save Data - R Tutorial