################################################################################ ### R BASICS WORKSHOP ### ### EXERCISE 4.1: Opening and Saving Data Tables ### ### ### ### Center for Conservation and Sustainable Development ### ### Missouri Botanical Garden ### ### Website: rbasicsworkshop.weebly.com ### ################################################################################ ## OBJECTIVE: ## In this exercise, you will practice data tables in R, and saving them into ## files. getwd() # With this command, you find what is the current working directory. This is the # folder in your computer that R uses for default to read and write files. # This means that if you do not provide an folder address to the opening/saving # functions, R will assume the files are or need to be in the working directory. # To change the working directory, you can use the "file" tab in PCs, # the "Session" tab in Rstudio or the "some other place" in Macs. # Alternatively, you can use the function 'setwd'. To give an address to this # (and other functions), you must provide a character string similar to this: # "Folder1/folder2/folder3" ## TASK 1. Change the working directory in your R session to a folder you want ## to use. # The main function to open a data table in R is read.table. The files that can # be opened by this function must text files, typically of extensions '.txt', # '.csv' or '.dat'. ## TASK 2. Using the function read.table, open the files ## 'NeotropicoOccidente_COL.txt' and 'NeotropicoOccidente_IGM2.txt'. ## Name the resulting objects "col" and "igm2". Note that these are ## comma-separated text files, and that the first row represents column names. ## TASK 3. Check the number of columns and rows of the objects that you just ## created with the contents of the files. Also, check what type of object ## "col" and "igm2" are. ## If you see that there is only one column, then you opened the file ## incorrectly. plot(igm2$PETmin, igm2$TOPOG) # Now that the files are open, you can do things with them. For example, this # Makes a plot of minimum potential evapo-transpiration (PETmin) against # topography in the Neotropics. plot(igm2$Lat, igm2$PETmin) # This one makes a figure showing the latitudinal variation in PETmin. # Now, lets suppose that you want to extract the residuals of a regression # between PETmin and latitude, and save them into a file. lm.results <- lm(igm2$PETmin~igm2$Lat+I(igm2$Lat^2)) lm.residuals <- residuals(lm.results) # This runs a polynomial regression, and then extracts the residuals into an # object named "lm.residuals" ## TASK 4. Use the function 'write.table' to save the object 'lm.residuals' into ## a tab-separated text file in your working directory. Name the file ## 'RegressionResiduals.txt". ## TASK 5. Open the file 'AdultLiteracy.xslx'. Save the contents of the ## file in an object of any name. Note here that the original files is an excel ## file. This means you first need to open it with excel, then save it as a text ## file, then use 'read.table' to open it in R. ## TASK 6. Open any other data table you have in your computer, maybe one with ## your own data. ################################################################################ ### TASK SOLUTIONS ############################################################# ################################################################################ ## TASK 1 ## setwd('Address/goes/here') # Numeric vector ## TASK 2 ## ## There are three main ways to do this: ## 1 col <- read.table(file="NeotropicoOccidente_COL.txt", header=TRUE, sep=",") igm2 <- read.table(file="NeotropicoOccidente_IGM2.txt", header=TRUE, sep=",") ## 2 col <- read.table(file=file.choose(), header=TRUE, sep=",") igm2 <- read.table(file=file.choose(), header=TRUE, sep=",") ## 3 col <- read.table(file="folder1/folder2/folder3/NeotropicoOccidente_COL.txt", header=TRUE, sep=",") igm2 <- read.table(file="folder1/folder2/folder3/NeotropicoOccidente_IGM2.txt", header=TRUE, sep=",") ## TASK 3 ## dim(col) dim(igm2) class(col) class(igm2) ## TASK 4 ## ## There are two main ways to do this: ## 1 write.table(x=lm.residuals, file="RegressionResiduals.txt", sep="\t") ## 2 write.table(x=lm.residuals, file="folder1/folder2/RegressionResiduals.txt", sep="\t") ## TASK 5 ## ## No solution here, sorry! :) ## TASK 6 ## ## No solution here, sorry! :)