R for Spatial Statistics

 

Files and Folders

In Review

Reading Tabular Data

Most of the time, you'll be loading tabular data (like data from a spreadsheet) into R. R makes this very easy but you need to select the right functions to read the data.

If you use one of the following "read" functions, you'll be able to read an entire file of tabular data into a "data frame". The "header" parameter indicates that the first line of the file contains header names for each column. You may see the "read.table()" function in examples but we've had problems with this function reading large tables. Instead, use "read.csv()" for Comma Seaprated Value (CSV) files and "read.delim" for all other text files with tabular data.

TheData=read.csv(FilePathWithFileName.csv,header=T)

After the read is complete, you can see the entire file had been imported into the variable "TheData". To access each of the columns within "TheData", use the "$" delimiter as below to plot an "X" and a "Y" column:

plot(TheData$X,TheData$Y) 

The "read.delim()" function allows you to read other files that use different delimiters. "sep" specifies the delimiter while "quote" specifies the type of quotes used to identify strings. The code below will read a tab-delimited file that uses double quotes for strings.

TheData=read.delim(FilePathWithFileName, header = TRUE, sep = "\t", qmethod = "\"")  

A lot of the data we read from files will have blank entries in cells. This can cause errors in R. You can set all the cells in a data frame or vector to 0's with the following:

TheMatrix[is.na(TheMatrix)] <- 0 // Get an array of indexes to blanks and set them to 0s.

Writing Tabular Data

Wrting tabular data is almost identical to reading tabular data, just add the data frame you want to write to the start of the parameters in the function call.

TheData=write.csv(DataFrameToWrite,FilePathWithFileName.csv,header=T)

The function below will write a data frame to a tab-delimited file.

TheData=write.delim(DataFrameToWrite,FilePathWithFileName,sep = "\t", qmethod = "\"")   

Reading and Writing Other R Objects

R can read and write other types of complex objects such as model outputs. The "save()" function will write objects to a file and "load()" will read them back again. The data is stored in a "binary" format that is only recognizable by R. The code below will save the objects "x" and "y" and the load it back again.

save(x,y,file="C:/Temp/XandY.RData")
load(file="C:/Temp/XandY.RData")

Lists of Files in the Directory/Folder

The "list" functions will provide a vector containing the names of the files or directories in a folder.

list.files(PathToTheFolder) # return a vector file names
list.dirs(PathToTheFolder) # return a vector of directory names


Interacting with the User

You can write scripts that ask the user for a file or folder of interest. These functions are part of the "tcltk" package.

tk_choose.dir(default="", caption="Select directory")
tk_choose.files(default = "", caption = "Select files") 

Workspaces

R always has a current "workspace" and some functions use this as the default location to read and write data. Once you set the workspace folder, you can specify "relative" paths to files.

setwd(PathToTheWorkspaceFolder)

Additional Resources