Tuesday, 25 September 2018

Importing Data Files from Other Software in R Language

Importing Data Files

Spreadsheet (Excel) file data

The xlsx package has the function read.xlsx ( ) for reading Excel files.

This will read the first sheet of an Excel spreadsheet.

To read Excel files, we first need to install the package

install.packages ("xlsx")
library (xlsx)
data  <-  read.xlsx ("datafile.xlsx", sheet Index or sheet Name)

 To load other sheets with read.xlsx( ), we specify a number for sheetIndex or a name for sheetName:

data  <- read.xlsx("datafile.xlsx", sheetIndex=2)

data  <-  read.xlsx("datafile.xlsx",sheetName="marks")

For reeading older Excel files in .xls format, use gdata package and function read.xls ( )

This will read the first sheet of an Excel spreadsheet.
To read Excel files, we first need to install the package

install.packages ("gdata")
library (gdata)
data  <- read.xls ("datafile.xls", sheet Index or sheet Name) )  

SPSS data file

For reading SPSs data files, use foreign package and function read.spss ( )

To read SPSs files, we first need to install the package

install.packages ("foreign")
library (foreign)
data  <-  read.spss ("datafile.sav") 


Other data files

The foreign package also includes functions to load other formats, including:
  • read.octave ("<Path to file>") : Octave and MATLAB
  • read.systat ("<Path to file>") : SYSTAT
  • read.xport ("<Path to file>") " SAS XPORT
  • read.data ("<Path to file>") : Stata
  
Contents of working directory

The list.files function shows the contents of your working directory:
> list.files ( )   // List of all available files in the working directory.

> setwd  ("C:/RCourse/")
> list.files ( )

 Redirecting Output to a File

Issue:
We want to redirect the output from R into a file instead of your console.

Solution:
Redirect the output of the cat function by using its file argument:

> ans  <-  6 + 8
> cat ("The answer of 6 + 8 is", ans, "\n", file="filename")

The Output will be saved in the working directory with given filename. 

Use the sink function to redirect all the output from both print and cat.

Call sink with a filename argument to begin redirecting console output to that file.

When we are done, sink with no argument to close the file and resume output to the console.

> sink ("filename")  #Beign writing output to file 
.....other session work .....
> sink

The print and cat functions normally write the output to console.

The cat function redirects the output to a file if we supply a file argument.

The print function cannot redirect its output.

The sink function can force all output to a file. 


Redirecting Output to a File: Three steps

1. 
> sink ("output.txt")  # Redirect output to file

2.
 > source ("script.R")  # Run the script, capture its output

3.
> sink ( )   # Resume writing output to console

Other options like append=TRUE/FALSE,  split=TRUE/FALSE are available. 

Example :-

 

0 Comments:

Post a Comment

Codecademy Code Foundations

Popular Posts

Categories

Android (23) AngularJS (1) Assembly Language (2) Books (11) C (75) C# (12) C++ (81) Course (3) Data Science (1) Data Strucures (4) Downloads (1) Engineering (13) flutter (1) FPL (17) Hadoop (1) HTML&CSS (40) IS (25) Java (89) Leet Code (4) Pandas (2) PHP (20) Projects (19) Python (435) R (69) Selenium Webdriver (2) Software (14) SQL (27)