R365: Day 30 – Set working directory and importing data from CSV files

I am helping my wife with more survival analysis, but one of the problems that I always run into is that I suck at opening up data sets from external files. What I’ve done in the past is create vectors for each column of data; however I have not had to do this with more than 3 columns. Even still, that alone is very obnoxious and it would be much easier to just directly import the data from the CSV file.

A lot of the help files for importing data suggest that you use the function read.csv(), which is fine and does work. However, when you go to specify your file, you can either correctly include the full directory name or you can set up a pre-determined directory. Or just fail, which is what I’ve done mostly in the past.

To set a working directory, you use the setwd() function. I was reading data off of an external drive, so I used:

# set up your working directory using setwd
#working directories are just wherever the file your interested in is stored
setwd('E:/R')
# remember to use a '/' not a '\' because '\' apparently means to escape
# this is especially pertinent if your are just copying whatever is at the bottom of the file description in properties
## you should have saved your file as a .csv before hand
## check out ??read.csv if you have other separators like tab or semicolon
survival<-read.csv(file='KM.csv',head=TRUE,sep=",")
#call the head of the file to make sure that the object is being read correctly
head(survival) ##It works!

Which worked! Kinda surprisingly. Now lets see if I can change the working directory, then call stuff from either directory (so that way if you have files in an external drive and your hard drive you don’t have to switch directories back and forth).

##set a new working directory
setwd('L:/Robin/R365')
##Try reading stuff out of the new directory...
packages<-read.csv(file='Rpackages.csv',head=TRUE,sep=",")
##...and the old E:/ directory
poster<-read.csv(file='postersummary.csv',head=TRUE,sep=",")
###...and it did not work. R's response is...
##In file(file, "rt") :
##cannot open file 'postersummary.csv': No such file or directory

This did not work out so well for me. I might be missing something or screwing up something else, but I suspect that R only understands one directory at a time if you are not including the whole file name (eg – L:/Robin/R365/rpackages.csv). Hope this helped!

EDIT**** – if setwd() doesnt work, use file.choose() it lets you pick the file from your system.

 

EDIT2****- once you have set a working drive, you can ask R what documents are in the drive using ‘ls()’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s