############################################################ ### R BASICS WORKSHOP ### ### PRESENTATION 4: OPENING AND SAVING DATA TABLES ### ### ### ### Center for Conservation and Sustainable Development ### ### Missouri Botanical Garden ### ### Website: rbasicsworkshop.weebly.com ### ### Last modification: 2021-June-04 ### ############################################################ ### A. WORKING DIRECTORY ################################### # The working directory is a predetermined folder in your # computer where R will write to or read files from. # To know which is the current working directory: getwd() ## METHOD 1 To change the working directory: using *setwd* # setwd(dir = "address to a folder on your computer"), # for example: setwd(dir="C:/Users/stello/Dropbox/Teaching/R_Basics_Workshop/") # IMPORTANT: # 1. the folder address is a single piece of text. # 2. the slashes are forward */*, not backwards *\* # 3. this address is specific to each computer! # 4. in your computer, you can get the address of a specific # file by going to the properties of the file (via a # right-click) getwd() # Confirms the change of working directory ## METHOD 2 To change the working directory: using menus # On Windows: File - Change Dir # On Mac: Misc - Change Working Directory # In R Studio: Session - Set Working Directory - # Choose Directory # The function "list.files" lists files and folders in the # specified directory: list.files() ### B. MAIN FUNCTIONS TO OPEN AND SAVE FILES ############### # 1. **read.table**: opens data frame from a text file # 2. *load*: opens any R object # 3. *source*: opens an R 'script' # 4. **write.table**: saves a data frame into a text file # 5. *save*: saves any R object ### C. OPENING A DATA TABLE ################################ help(read.table) ## MAIN ARGUMENTS: # 1. file: Character with the name of the file to open - # and the address if the file is not in the working # directory. # 2. header: TRUE or FALSE. TRUE if the first row in the # file represents the names of the columns. # 3. sep: The character used to separate values, more # often: "\t" or "," (for tab-separated files or # comma-separated files respectively). ## STEP 1. SAVE AN EXCEL FILE AS A TEXT FILE ############### # 1.1. Open the file in excel # 1.2. Select "save as". Choose one of two options: text # tab delimited, or CSV comma delimited # 1.3. Close and save the file from excel ## STEP 2. USE THE FUNCTION *read.table* ################### ## Option 2.1 (easy): using *file.choose()* to browse a ## file to open streb.data <- read.table(file=file.choose(), header=TRUE, sep="\t") # When using *read.table*, it is important to make sure you # are using the right value for argument *sep*. If you # saved your file as tab delimited text, then *sep="\t"*. If # you saved the file as comma separated, then *sep=","*. ## IMPORTANT: Note that the result of *read.table* is ## assigned to an object. This object will now ## have the contents of the file. # Option 2.2 (intermediate): place the file in the working # directory to open it directly getwd() # This tells you what the current working # directory is. # This created an object named *dir.to.open* which has an # folder address, which will become the working directory. dir.to.open <- "C:/Users/stello/Dropbox/Teaching/R_Basics_Workshop/RBasicsWorkshopMaterials/3_Datasets" # This changes the current working directory to the folder # addressed in *dir.to.open*. setwd(dir.to.open) # Now, the argument *file* is only the name of the file # "streblidaeonbats.txt", which can be opened because it is # in the working directory. streb.data <- read.table(file="streblidaeonbats.txt", header=TRUE, sep="\t") # Option 2.3 (most flexible): passing the address and file # name to the argument *file* in *read.table*. streb.data <- read.table( file=paste(dir.to.open, "streblidaeonbats.txt", sep="/"), header=TRUE, sep="\t") # Note here that we are pasting the address to the file # name, and then passing that to the argument *file*. ## STEP 3. CHECK THAT THE FILE HAS BEEN OPENED CORRECTLY ### ## IMPORTANT: *read.table* always opens a file as a ## "data.frame" class(streb.data) dim(streb.data) names(streb.data) head(streb.data) ### D. SAVE A DATA TABLE ################################### help(write.table) ## MAIN ARGUMENTS: # 1. x: R object (data frame, matrix or vector) to write # into a file. # 2. file: Character with the name of the file to create - # and the address if the file is not in the working # directory. # 3. sep: The character used to separate values, more # often: "\t" or ",". abund.data <- data.frame(letters, rnorm(length(letters))) colnames(abund.data) <- c("sp", "measure") rownames(abund.data) <- paste("ind", 1:nrow(abund.data)) abund.data ## Option 1: save the table into the working directory. write.table(x=abund.data, file="abund_data.txt", sep="\t") ## Option 2: save the table into a different directory dir.to.write <- "C:/Users/stello/Dropbox/Teaching/R_Basics_Workshop/RBasicsWorkshopMaterials/3_Datasets" write.table(x=abund.data, file=paste(dir.to.write, "abund_data.txt", sep="/"), sep="\t")