9.6 Additional tips

  1. There are many functions other than read.table() for importing data. For example, the functions read.csv and read.delim are specific for importing comma-separated and tab-separated text files. In practice, these functions do the same thing as read.table, but they don’t require you to specify a sep argument. Personally, I always use read.table() because it always works and I don’t like trying to remember unnecessary functions.

##Test your R Might!

  1. 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.

  2. Outside of RStudio, navigate to the directory you selected in Question 1 and create three new folders – Call them data,R, andnotes`.

  3. Go back to RStudio and open a new R script. Save the script as CreatingObjects.R in the R folder you created in Question 2.

  4. In the script, create new objects called a, b, and c. 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)
  1. 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.

  2. I have a tab–delimited text file called club at the following web address: http://nathanieldphillips.com/wp-content/uploads/2015/12/club.txt. Using read.table(), load the data as a new object called club.df in your workspace.

  3. Using write.table(), save the dataframe as a tab–delimited text file called club.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.

  4. Save the three objects a, b, c, and club.df to an .RData file called “myobjects.RData” in your data folder using save().

  5. Clear your workspace using the rm(list = ls()) function. Then, run the ls() function to make sure the objects are gone.

  6. Open a new R script called AnalyzingObjects.R and save the script to the R folder you created in Question 2.

  7. Now, in your AnalyzingObjects.R script, load the objects back into your workspace from the myobjects.RData file using the load() function. Again, run the ls() function to make sure all the objects are back in your workspace.

  8. Add some R code to your AnalyzingObjects.R script. Calculate some means and percentages. Now save your AnalyzingObjects.R script, and then save all the objects in your workspace to myobjects.RData.

  9. Congratulations! You are now a well-organized R Pirate! Quit RStudio and go outside for some relaxing pillaging.