9.6 Additional tips
- There are many functions other than
read.table()
for importing data. For example, the functionsread.csv
andread.delim
are specific for importing comma-separated and tab-separated text files. In practice, these functions do the same thing asread.table
, but they don’t require you to specify asep
argument. Personally, I always useread.table()
because it always works and I don’t like trying to remember unnecessary functions.
##Test your R Might!
In RStudio, open a new R Project in a new directory by clicking File – New Project. Call the directory
MyRProject
, and then select a directory on your computer for the project. This will be the project’s working directory.Outside of RStudio, navigate to the directory you selected in Question 1 and create three new folders – Call them
data,
R, and
notes`.Go back to RStudio and open a new R script. Save the script as
CreatingObjects.R
in theR
folder you created in Question 2.In the script, create new objects called
a
,b
, andc
. You can assign anything to these objects – from vectors to dataframes. If you can’t think of any, use these:
a <- data.frame("sex" = c("m", "f", "m"),
"age" = c(19, 43, 25),
"favorite.movie" = c("Moon", "The Goonies", "Spice World"))
b <- mean(a$age)
c <- table(a$sex)
Send the code to the Console so the objects are stored in your current workspace. Use the
ls()
function to see that the objects are indeed stored in your workspace.I have a tab–delimited text file called
club
at the following web address: http://nathanieldphillips.com/wp-content/uploads/2015/12/club.txt. Usingread.table()
, load the data as a new object calledclub.df
in your workspace.Using
write.table()
, save the dataframe as a tab–delimited text file calledclub.txt
to the data folder you created in Question 2. Note: You won’t use the text file again for this exercise, but now you have it handy in case you need to share it with someone who doesn’t use R.Save the three objects
a
,b
,c
, andclub.df
to an .RData file called “myobjects.RData” in your data folder usingsave()
.Clear your workspace using the
rm(list = ls())
function. Then, run thels()
function to make sure the objects are gone.Open a new R script called
AnalyzingObjects.R
and save the script to theR
folder you created in Question 2.Now, in your
AnalyzingObjects.R
script, load the objects back into your workspace from themyobjects.RData
file using theload()
function. Again, run thels()
function to make sure all the objects are back in your workspace.Add some R code to your
AnalyzingObjects.R
script. Calculate some means and percentages. Now save yourAnalyzingObjects.R
script, and then save all the objects in your workspace tomyobjects.RData
.Congratulations! You are now a well-organized R Pirate! Quit RStudio and go outside for some relaxing pillaging.