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

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses