This is a living document, so will be managed and updated as needed by the community. The published version on RPubs is here: http://rpubs.com/crt34/faqs.


Getting Set-Up with R & RStudio


Beginner Resources by Topic






Specialist Topics


Operational Basics


FAQs

Getting Your Data into R

1. Loading Existing Local Data

(a) When already in the working directory where the data is

Import a local csv file (i.e. where data is separated by commas), saving it as an object:

#this will create a data frame called "object"
#the header argument is defaulted to TRUE, i.e. read.csv assumes your file has a header row and will take the first row of your csv to be the column names
object <- read.csv("xxx.csv")

#if your csv does not have a header row, add header = FALSE to the command
#in this call default column headers will be assigned which can be changed
object <- read.csv("xxx.csv", header = FALSE)

Import a local tab delimited file (i.e. where data is separated by tabs), saving is as an object:

#this will create a data frame called "object"
#the header argument is defaulted to TRUE, i.e. read.csv assumes your file has a header row and will take the first row of your csv to be the column names
object <- read.table("xxx.txt", sep = "\t")

#if your csv does not have a header row, add header = FALSE to the command
#in this call default column headers will be assigned which can be changed
object <- read.table("xxx.txt", sep = "\t", header = FALSE)
(b) When NOT in the working directory where the data is

For example to import and save a local csv file from a different working directory you can either need to specify the file path (operating system specific), e.g.:

#on a mac
object <- read.csv("~/Desktop/R/data.csv")

#on windows
object <- read.csv("C:/Desktop/R/data.csv")

OR

You can use the file.choose() command which will interactively open up the file dialog box for you to browse and select the local file, e.g.:

object <- read.csv(file.choose())
(c) Copying and Pasting Data

For relatively small amounts of data you can do an equivalent copy paste (operating system specific):

#on a mac
object <- read.table(pipe("pbpaste"))

#on windows
object <- read.table(file = "clipboard")

2. Loading Non-Numerical Data - character strings

Be careful when loading text data! R may assume character strings are statistical factor variables, e.g. “low”, “medium”, “high”, when are just individual labels like names. To specify text data NOT to be converted into factor variables, add stringsAsFactor = FALSE to your read.csv/read.table command:

object <- read.table("xxx.txt", stringsAsFactors = FALSE)

3. Downloading Remote Data

For accessing files from the web you can use the same read.csv/read.table commands. However, the file being downloaded does need to be in an R-friendly format (maximum of 1 header row, subsequent rows are the equivalent of one data record per row, no extraneous footnotes etc.). Here is an example downloading an online csv file from Pew Research:

object <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/datasets/AirPassengers.csv")

4. Other Formats - Excel, SPSS, SAS etc.

For other file formats, you will need specific R packages to import these data.

Here’s a good site for an overview: http://www.statmethods.net/input/importingdata.html

Here’s a more detailed site: http://r4stats.com/examples/data-import/

Here’s some info on the foreign package for loading statistical software file types: http://www.ats.ucla.edu/stat/r/faq/inputdata_R.htm


Getting Your Data out of R

1. Exporting data

Navigate to the working directory you want to save the data table into, then run the command (in this case creating a tab delimited file):

write.table(object, "xxx.txt", sep = "\t")

2. Save down an R object

Navigate to the working directory you want to save the object in then run the command:

save(object, file = "xxx.rda")

#reload the object
load("xxx.rda")