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

Loading your data into R with rio packages

The rio package is a relatively recent R package, developed by Thomas J. Leeper, which makes data import and export in R painless and quick.

This objective is mainly reached when rio makes assumptions about the file format. This means that the rio package guesses the format of the file you are trying to import and consequently applies import functions appropriate to that format.

All of this is done behind the scenes, and the user is just required to run the import() function.

As Leeper often states when talking about the package: "it just works."

One of the great results you can obtain by employing this package is streamlining workflows involving different development and productivity tools.

For instance, it is possible to produce tables directly into sas and make them available to the R environment without any particular export procedure in sas, we can directly acquire data in R as it is produced, or input into an Excel spreadsheet.

Getting ready

As you would expect, we first need to install and load the rio package:

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

In the following example, we are going to import our well-known world_gdp_data dataset from a local .csv file.

How to do it...

  1. The first step is to import the dataset using the import() function:
    messy_gdp ← import("world_gdp_data.csv")
  2. Then, we visualize the result with the RStudio viewer:
    View(messy_gdp)

How it works...

We first import the dataset using the import() function. To understand the structure of the import() function, we can leverage a useful behavior of the R console: putting a function name without parentheses and running the command will result in the printing of all the function definitions.

Running the import on the R console will produce the following output:

function (file, format, setclass, ...) 
{
    if (missing(format)) 
        fmt <- get_ext(file)
    else fmt <- tolower(format)
    if (grepl("^http.*://", file)) {
        temp_file <- tempfile(fileext = fmt)
        on.exit(unlink(temp_file))
        curl_download(file, temp_file, mode = "wb")
        file <- temp_file
    }
    x <- switch(fmt, r = dget(file = file), tsv = import.delim(file = file, 
        sep = "\t", ...), txt = import.delim(file = file, sep = "\t", 
        ...), fwf = import.fwf(file = file, ...), rds = readRDS(file = file, 
        ...), csv = import.delim(file = file, sep = ",", ...), 
        csv2 = import.delim(file = file, sep = ";", dec = ",", 
            ...), psv = import.delim(file = file, sep = "|", 
            ...), rdata = import.rdata(file = file, ...), dta = import.dta(file = file, 
            ...), dbf = read.dbf(file = file, ...), dif = read.DIF(file = file, 
            ...), sav = import.sav(file = file, ...), por = read_por(path = file), 
        sas7bdat = read_sas(b7dat = file, ...), xpt = read.xport(file = file), 
        mtp = read.mtp(file = file, ...), syd = read.systat(file = file, 
            to.data.frame = TRUE), json = fromJSON(txt = file, 
            ...), rec = read.epiinfo(file = file, ...), arff = read.arff(file = file), 
        xls = read_excel(path = file, ...), xlsx = import.xlsx(file = file, 
            ...), fortran = import.fortran(file = file, ...), 
        zip = import.zip(file = file, ...), tar = import.tar(file = file, 
            ...), ods = import.ods(file = file, ...), xml = import.xml(file = file, 
            ...), clipboard = import.clipboard(...), gnumeric = stop(stop_for_import(fmt)), 
        jpg = stop(stop_for_import(fmt)), png = stop(stop_for_import(fmt)), 
        bmp = stop(stop_for_import(fmt)), tiff = stop(stop_for_import(fmt)), 
        sss = stop(stop_for_import(fmt)), sdmx = stop(stop_for_import(fmt)), 
        matlab = stop(stop_for_import(fmt)), gexf = stop(stop_for_import(fmt)), 
        npy = stop(stop_for_import(fmt)), stop("Unrecognized file format"))
    if (missing(setclass)) {
        return(set_class(x))
    }
    else {
        a <- list(...)
        if ("data.table" %in% names(a) && isTRUE(a[["data.table"]])) 
            setclass <- "data.table"
        return(set_class(x, class = setclass))
    }
}

As you can see, the first task performed by the import() function calls the get_ext() function, which basically retrieves the extension from the filename.

Once the file format is clear, the import() function looks for the right subimport function to be used and returns the result of this function.

Next, we visualize the result with the RStudio viewer. One of the most powerful RStudio tools is the data viewer, which lets you get a spreadsheet-like view of your data.frame objects. With RStudio 0.99, this tool got even more powerful, removing the previous 1000-row limit and adding the ability to filter and format your data in the correct order.

When using this viewer, you should be aware that all filtering and ordering activities will not affect the original data.frame object you are visualizing.

There's more...

As fully illustrated within the Rio vignette (which can be found 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.

主站蜘蛛池模板: 介休市| 岗巴县| 栾城县| 仪征市| 宣化县| 乌鲁木齐市| 灌南县| 明光市| 资兴市| 广安市| 沙坪坝区| 定陶县| 庆安县| 双江| 镇雄县| 上犹县| 赤峰市| 鲁甸县| 济源市| 都江堰市| 木兰县| 改则县| 农安县| 宜章县| 永川市| 彭州市| 信阳市| 丰顺县| 蒲江县| 平昌县| 深州市| 安图县| 利津县| 湟源县| 博乐市| 皮山县| 和静县| 盐津县| 西峡县| 赞皇县| 布尔津县|