官术网_书友最值得收藏!

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:

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.

主站蜘蛛池模板: 板桥市| 永胜县| 弋阳县| 独山县| 上栗县| 长宁区| 金昌市| 绥阳县| 临漳县| 札达县| 福贡县| 白山市| 剑阁县| 潍坊市| 定安县| 伽师县| 全椒县| 榆社县| 砀山县| 田林县| 甘洛县| 东城区| 正安县| 蒙城县| 孙吴县| 西乌珠穆沁旗| 财经| 英德市| 抚州市| 武乡县| 南郑县| 关岭| 鲁山县| 建始县| 宜州市| 栖霞市| 黄平县| 河间市| 新安县| 临夏县| 望都县|