Skip to Navigation Skip to Content Skip to Search Skip to Site Map
Search

Importing Data Examples – 17th February 2017

 

An 10 minute example showing importing 3 files into R:

  1. a relatively easy to read csv file into R, which shows the general process and a few settings
  2. a “worst case” tab delimited text file with several tricky areas, which runs over some of the error messages you might see and what they mean
  3. using RStudio’s import option which has changed a bit in the latest version.

You can watch the recording (10 minutes 10 seconds)

 

or download (right click and choose the save/download link option) the recording to your computer via the link:

RImportData (.mp4 format, 117.6 MB)

and read the parts of the script below (code available when viewing the full article)

#set the working directory and provide the filename to read.csv
read.csv("QEX350601_20170217_122115_8.csv")
#store the result
qex <- read.csv("QEX350601_20170217_122115_8.csv")
#skip the top line
qex <- read.csv("QEX350601_20170217_122115_8.csv", skip=1)
#check the result
str(qex)
#store text as characters rather than factors
qex <- read.csv("QEX350601_20170217_122115_8.csv", skip=1, stringsAsFactors = FALSE)
str(qex)

#Example 2 worst case
#try a read
election <- read.table("e9_part9_1.txt")
#go find out what program saved the file and the format of the text
election <- read.table("e9_part9_1.txt", fileEncoding="UTF-16")
#get the seperator between columns correct
election <- read.table("e9_part9_1.txt", fileEncoding="UTF-16", sep="\t")
#skip lines and set strings to factors
election <- read.table("e9_part9_1.txt", fileEncoding="UTF-16", sep="\t", skip=5, stringsAsFactors = FALSE)

#example three new RStudio

 

Leave a comment