Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
RStudio for R Statistical Computing Cookbook

You're reading from   RStudio for R Statistical Computing Cookbook Over 50 practical and useful recipes to help you perform data analysis with R by unleashing every native RStudio feature

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher
ISBN-13 9781784391034
Length 246 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrea Cirillo Andrea Cirillo
Author Profile Icon Andrea Cirillo
Andrea Cirillo
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Acquiring Data for Your Project 2. Preparing for Analysis – Data Cleansing and Manipulation FREE CHAPTER 3. Basic Visualization Techniques 4. Advanced and Interactive Visualization 5. Power Programming with R 6. Domain-specific Applications 7. Developing Static Reports 8. Dynamic Reporting and Web Application Development Index

Converting file formats using the rio package

As we saw in the previous recipe, Rio is an R package developed by Thomas J. Leeper which makes the import and export of data really easy. You can refer to the previous recipe for more on its core functionalities and logic.

Besides the import() and export() functions, Rio also offers a really well-conceived and straightforward file conversion facility through the convert() function, which we are going to leverage in this recipe.

Getting ready

First of all, we need to install and make the rio package available by running the following code:

install.packages("rio")
library(rio)

In the following example, we are going to import the world_gdp_data dataset from a local .csv file. This dataset is provided within the RStudio project related to this book, in the data folder.

You can download it by authenticating your account at http://packtpub.com.

How to do it...

  1. The first step is to convert the file from the .csv format to the .json format:
    convert("world_gdp_data.csv", "world_gdp_data.json")

    This will create a new file without removing the original one.

  2. The next step is to remove the original file:
    file.remove("world_gdp_data.csv")

There's more...

As fully illustrated within the Rio vignette (which you can find at https://cran.r-project.org/web/packages/rio/vignettes/rio.html), the following formats are supported for import and export:

Format

Import

Export

Tab-separated data (.tsv)

Yes

Yes

Comma-separated data (.csv)

Yes

Yes

CSVY (CSV + YAML metadata header) (.csvy)

Yes

Yes

Pipe-separated data (.psv)

Yes

Yes

Fixed-width format data (.fwf)

Yes

Yes

Serialized R objects (.rds)

Yes

Yes

Saved R objects (.RData)

Yes

Yes

JSON (.json)

Yes

Yes

YAML (.yml)

Yes

Yes

Stata (.dta)

Yes

Yes

SPSS and SPSS portable

Yes (.sav and .por)

Yes (.sav only)

XBASE database files (.dbf)

Yes

Yes

Excel (.xls)

Yes

 

Excel (.xlsx)

Yes

Yes

Weka Attribute-Relation File Format (.arff)

Yes

Yes

R syntax (.r)

Yes

Yes

Shallow XML documents (.xml)

Yes

Yes

SAS (.sas7bdat)

Yes

 

SAS XPORT (.xpt)

Yes

 

Minitab (.mtp)

Yes

 

Epiinfo (.rec)

Yes

 

Systat (.syd)

Yes

 

Data Interchange Format (.dif)

Yes

 

OpenDocument Spreadsheet (.ods)

Yes

 

Fortran data (no recognized extension)

Yes

 

Google Sheets

Yes

 

Clipboard (default is .tsv)

  

Since rio is still a growing package, I strongly suggest that you follow its development on its GitHub repository, where you will easily find out when new formats are added, at https://github.com/leeper/rio.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image