Importing Different Types of Files

The R Project for Statistical Computing (available in Windows, Mac OS, and Linux)

Importing Different Types of Files

Postby meninonas » Thu Dec 04, 2014 12:17 am

The first thing to do before importing data is knowing what directory R is working from. You can see where R will be extracting all data from with the following command:

> getwd() # get current working directory

If you are interested in changing it to where your data is going to be located, that can be changed through:

> setwd("<new path>") # set working directory

TEXT FILES:

The data I've been working with is attached under Weight-BP-Disease-2.txt.

> mydata = read.table("mydata.txt") # read text file

EXCEL FILES:

> install.packages(gdata) #installing
> library(gdata) # load gdata package
> help(read.xls) # documentation
> mydata = read.xls("mydata.xls") # read from first sheet

SPSS FILES:

For SPSS files, you need to save it as portable, or with the .por extension. Afterwards,

library(Hmisc)
mydata <- spss.get("c:/mydata.por", use.value.labels=TRUE)

SAS FILES:

You need to save SAS dataset in trasport format. The following command should work.

libname out xport 'c:/mydata.xpt';
data out.mydata;
set sasuser.mydata;
run;

In R, you run the following commands,

library(Hmisc)
mydata <- sasxport.get("c:/mydata.xpt")
Attachments
weight-BP-disease.txt
(860 Bytes) Downloaded 169 times
meninonas
 
Posts: 137
Joined: Tue Jun 24, 2014 3:25 pm

Return to R

Who is online

Users browsing this forum: No registered users and 1 guest

cron