The working directory is the folder on your local computer that you would like to interact with (read and write to) during your RStudio session. The getwd() function can be used to obtain your current working directory.
getwd()
## [1] "C:/Users/chh35/OneDrive - Drexel University/Teaching/Drexel/STAT 331/Course Content/Week 1"
You may want to change your working directory. You can use the setwd() function to change your working directory. Note: When identifying your path, we use Linux/Mac style, using forward slashes (‘/’), regardless of operating system.
setwd("C:/Users/chh35/OneDrive - Drexel University/Teaching/Drexel/STAT 331/Course Content/Week 1/")
Once you set the correct working directory, you can refer to any files in that location by name (and file extension), when referring to them in R.
R has some built-in datasets, one of which (ChickWeight) we will use to demonstrate reading and writing files from/to the working directory. We can use the data() function to load a built-in data set.
data("ChickWeight")
We can view the first 6 rows of the ChickWeight dataframe using the head() function
head(ChickWeight)
## weight Time Chick Diet
## 1 42 0 1 1
## 2 51 2 1 1
## 3 59 4 1 1
## 4 64 6 1 1
## 5 76 8 1 1
## 6 93 10 1 1
We use the write.csv() function to export data from R as a CSV (.csv) file. We can write comma-separated values for the dataframe ChickWeight and save them in a file called “chkweights.csv” in the current working directory.
write.csv(x = ChickWeight, #dataframe to export
file = "chkweights.csv", # file name
row.names = FALSE) # exclude row names
We use the read.csv() function to import a CSV file into R. The data will be automatically coerced to a dataframe. By default, R will use the first row of the file as column names (header = TRUE). Below, we import the “chkweights.csv” file into R as a dataframe named chkweights.
chkweights <- read.csv(file = "chkweights.csv")
If we have a link to a CSV file online, we can read it directly from the URL. We set the argument stringsAsFactors = TRUE to convert columns with text to categorical (factor) variables.
store.df <- read.csv("http://goo.gl/QPDdMl",
stringsAsFactors = TRUE)
head(store.df)
## storeNum Year Week p1sales p2sales p1price p2price p1prom p2prom country
## 1 101 1 1 127 106 2.29 2.29 0 0 US
## 2 101 1 2 137 105 2.49 2.49 0 0 US
## 3 101 1 3 156 97 2.99 2.99 1 0 US
## 4 101 1 4 117 106 2.99 3.19 0 0 US
## 5 101 1 5 138 100 2.49 2.59 0 1 US
## 6 101 1 6 115 127 2.79 2.49 0 0 US
You can also export data as a text file using the write.table() function. By default, the values are space delimited (sep = " "). We can change the delimiter by using the sep argument.
write.table(x = store.df, # dataframe to export
file = "storedf.txt", # TXT file name and extension
row.names = FALSE) # exclude row names
Next, we can import the text file, using the read.table() function. By default, header = FALSE. If the first row contains column header (names) information, change to header = TRUE.
storetxt <- read.table(file = "storedf.txt", # TXT file to import
header = TRUE) # set row 1 as column names
head(storetxt)
## storeNum Year Week p1sales p2sales p1price p2price p1prom p2prom country
## 1 101 1 1 127 106 2.29 2.29 0 0 US
## 2 101 1 2 137 105 2.49 2.49 0 0 US
## 3 101 1 3 156 97 2.99 2.99 1 0 US
## 4 101 1 4 117 106 2.99 3.19 0 0 US
## 5 101 1 5 138 100 2.49 2.59 0 1 US
## 6 101 1 6 115 127 2.79 2.49 0 0 US
Objects in the RStudio workspace (global environment) can be saved as an RData file. RData files can be imported and exported for use in an RStudio session.
Before working with RData, we can learn more about the workspace. To view the objects in our current workspace, we can use the ls() function, with no arguments specified.
ls()
## [1] "ChickWeight" "chkweights" "store.df" "storetxt"
To save individual objects from our workspace in a .RData file in our current working directory, we use the save() function.
save(chkweights, store.df,
file = "Week1.RData")
Next, we can remove the objects from our workspace using the rm() function.
rm(chkweights, store.df)
ls()
## [1] "ChickWeight" "storetxt"
To import the objects contained in an RData file into our current workspace, we use the load() function, specifying the name of the .RData file.
load(file = "Week1.RData")
We can confirm that the objects in “Week1.RData” are back in our workspace.
ls()
## [1] "ChickWeight" "chkweights" "store.df" "storetxt"
The entire workspace can be saved as a .RData object using the save.image() function.
save.image(file = "All_Week1.RData")
In order to remove all objects from our workspace, we can use rm(list = ls()), which will remove all of the contents of ls() (our current workspace) using the rm() function. Be careful when doing this, once the objects are removed they cannot be retrieved unless they were previously saved to an RData file.
rm(list = ls())
ls()
## character(0)
Finally, we can load the workspace objects that we save in the “All_Week1.RData” file back in again using the load() function and confirm that they are in the workspace using the ls() function.
load(file = "All_Week1.RData")
ls()
## [1] "ChickWeight" "chkweights" "store.df" "storetxt"